Skip to content

Commit f0693cf

Browse files
authored
check user permissions using "permissions" field (#130)
* use only "permissions" to determine whether user has write access
1 parent be4cf48 commit f0693cf

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

mergin/client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,17 @@ def resolve_unfinished_pull(self, directory):
848848
mp = MerginProject(directory)
849849
conflicts = mp.resolve_unfinished_pull(self.username())
850850
return conflicts
851+
852+
def has_writing_permissions(self, project_path):
853+
"""
854+
Test whether user has writing permissions on the given project.
855+
We rely on the "permissions" field here, as it provides more accurate
856+
information and take into account namespace permissions too.
857+
858+
:param project_path: project's path on server in the form or namespace/project
859+
:type directory: str
860+
:returns: whether user has writing (upload) permission on the project
861+
:rtype: bool
862+
"""
863+
info = self.project_info(project_path)
864+
return info["permissions"]["upload"]

mergin/client_push.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ def push_project_async(mc, directory):
101101
mp.log.info(f"got project info: local version {local_version} / server version {server_version}")
102102

103103
username = mc.username()
104-
if username not in server_info["access"]["writersnames"]:
104+
# permissions field contains information about update, delete and upload privileges of the user
105+
# on a specific project. This is more accurate information then "writernames" field, as it takes
106+
# into account namespace privileges. So we have to check only "permissions", namely "upload" one
107+
if not mc.has_writing_permissions(project_path):
105108
mp.log.error(f"--- push {project_path} - username {username} does not have write access")
106109
raise ClientError(f"You do not seem to have write access to the project (username '{username}')")
107110

mergin/test/test_client.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,6 @@ def test_project_versions_list(mc):
16251625
assert versions[0]["name"] == "v2"
16261626
assert versions[-1]["name"] == "v4"
16271627

1628-
16291628
def test_report(mc):
16301629
test_project = 'test_download_diffs'
16311630
project = API_USER + '/' + test_project
@@ -1662,3 +1661,45 @@ def test_report(mc):
16621661
shutil.rmtree(directory)
16631662
with pytest.raises(InvalidProject):
16641663
create_report(mc, directory, since, to, report_file)
1664+
1665+
def test_project_versions_list(mc, mc2):
1666+
"""
1667+
Test retrieving user permissions
1668+
"""
1669+
test_project = 'test_permissions'
1670+
test_project_fullname = API_USER2 + '/' + test_project
1671+
1672+
# cleanups
1673+
project_dir = os.path.join(TMP_DIR, test_project, API_USER)
1674+
cleanup(mc, test_project_fullname, [project_dir])
1675+
cleanup(mc2, test_project_fullname, [project_dir])
1676+
1677+
# create new (empty) project on server
1678+
mc2.create_project(test_project)
1679+
1680+
# Add reader access to another client
1681+
project_info = get_project_info(mc2, API_USER2, test_project)
1682+
access = project_info['access']
1683+
access['readersnames'].append(API_USER)
1684+
mc2.set_project_access(test_project_fullname, access)
1685+
1686+
# reader should not have write access
1687+
assert not mc.has_writing_permissions(test_project_fullname)
1688+
1689+
# Add writer access to another client
1690+
project_info = get_project_info(mc2, API_USER2, test_project)
1691+
access = project_info['access']
1692+
access['writersnames'].append(API_USER)
1693+
mc2.set_project_access(test_project_fullname, access)
1694+
1695+
# now user shold have write access
1696+
assert mc.has_writing_permissions(test_project_fullname)
1697+
1698+
# test organization permissions
1699+
test_project_fullname = 'testorg' + '/' + 'test_org_permissions'
1700+
1701+
# owner should have write access
1702+
assert mc.has_writing_permissions(test_project_fullname)
1703+
1704+
# writer should have write access
1705+
assert mc2.has_writing_permissions(test_project_fullname)

0 commit comments

Comments
 (0)