Added modify_access call + CLI interface
authorSybren A. Stüvel <sybren@stuvel.eu>
Fri, 3 Nov 2017 12:55:13 +0000 (13:55 +0100)
committerSybren A. Stüvel <sybren@stuvel.eu>
Fri, 3 Nov 2017 12:55:13 +0000 (13:55 +0100)
svnman/cli.py
svnman/remote.py

index 50982d9..2d7726e 100644 (file)
@@ -25,4 +25,30 @@ def info(repo_id):
     log.info('Access : %s', sorted(repoinfo.access))
 
 
+@manager_svnman.command
+def grant(repo_id, username, password):
+    """Allows the user access to the repository."""
+
+    import bcrypt
+    from . import current_svnman
+
+    hashed = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt()).decode('ascii')
+
+    log.info('Granting access to repo %r', repo_id)
+    log.info('Hashed password: %r', hashed)
+    current_svnman.remote.modify_access(repo_id, grant=[(username, hashed)], revoke=[])
+    log.info('Done')
+
+
+@manager_svnman.command
+def revoke(repo_id, username):
+    """Revokes the user access from the repository."""
+
+    from . import current_svnman
+
+    log.info('Revoking access from repo %r', repo_id)
+    current_svnman.remote.modify_access(repo_id, grant=[], revoke=[username])
+    log.info('Done')
+
+
 manager.add_command('svn', manager_svnman)
index 0aee5f6..1e4b363 100644 (file)
@@ -57,3 +57,24 @@ class API(object):
 
         auth = (self.username, self.password) if self.username or self.password else None
         return self._session.request(method, abs_url, auth=auth, **kwargs)
+
+    def modify_access(self,
+                      repo_id: str,
+                      grant: typing.List[typing.Tuple[str, str]],
+                      revoke: typing.List[str]):
+        """Modifies user access to the repository.
+
+        Does not return anything; no exception means exection was ok.
+
+        :param repo_id: the repository ID
+        :param grant: list of (username password) tuples. The passwords should be BCrypt-hashed.
+        :param revoke: list of usernames.
+        """
+
+        grants = [{'username': u, 'password': p} for u, p in grant]
+
+        resp = self._request('POST', f'repo/{repo_id}/access', json={
+            'grant': grants,
+            'revoke': revoke,
+        })
+        resp.raise_for_status()