From 030b5991adc429560793cb91819ed3762d9adada Mon Sep 17 00:00:00 2001 From: David Macro Date: Fri, 28 Jul 2017 17:20:49 +0200 Subject: [PATCH] When deleting an user, remove it from all the groups it belongs to --- pyolite/managers/user.py | 7 +++++-- pyolite/models/user.py | 30 ++++++++++++++++++------------ setup.py | 2 +- tests/test_group.py | 18 +++++++++++++++++- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/pyolite/managers/user.py b/pyolite/managers/user.py index e65a982..ef96eaa 100644 --- a/pyolite/managers/user.py +++ b/pyolite/managers/user.py @@ -2,7 +2,8 @@ from unipath import Path from pyolite.abstracts import Manager -from pyolite import User +from pyolite import Group, Repository, User +from pyolite.managers import GroupManager class UserManager(Manager): @@ -23,7 +24,9 @@ def delete(self, name): return dest = Path(self.path, 'keydir/%s' % name) for repo in user.repos: - repo.users.remove(user.name) + Repository.get(repo, self.path, self.git).users.remove(user.name) + for group in user.groups: + GroupManager(self.path).user_delete(group, user.name) if dest.exists(): dest.rmtree() self.git.commit([str(dest)], 'Deleted user %s.' % name) diff --git a/pyolite/models/user.py b/pyolite/models/user.py index 98087e8..b5668f1 100644 --- a/pyolite/models/user.py +++ b/pyolite/models/user.py @@ -1,3 +1,4 @@ +import os import re from six import string_types from unipath import Path @@ -13,6 +14,7 @@ def __init__(self, name, path, git, **kwargs): self.regex = re.compile(r'=( *)(\w+)') # self.repos = kwargs.get('repos') or [] + self.groups = kwargs.get('groups') or [] self.keys = ListKeys(self, kwargs.get('keys') or []) @classmethod @@ -22,18 +24,22 @@ def get_by_name(cls, name, path, git): keys = [key for key in key_path.walk() if key.endswith('%s.pub' % name)] # get user's repos - repos = [] - repos_path = Path(path, 'conf/') - for repo in repos_path.walk(): - if repo.isdir(): - continue - - with open(str(repo)) as f: - if name in f.read(): - repos.append(repo) - - if repos or keys: - return cls(name, path, git, repos=repos, keys=keys) + def get_objects(suffix): + objects = [] + for obj in Path(path, 'conf', suffix).walk(): + if obj.isdir(): + continue + with open(str(obj)) as f: + if name in f.read(): + filename = os.path.splitext(os.path.basename(str(obj)))[0] + objects.append(filename) + return objects + # get user's repos and groups + repos = get_objects('repos') + groups = get_objects('groups') + + if repos or keys or groups: + return cls(name, path, git, repos=repos, keys=keys, groups=groups) return None @classmethod diff --git a/setup.py b/setup.py index 6da7036..ee963aa 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup, find_packages setup(name="baguette-olite", - version="0.12", + version="0.13", author_email="pydavid@baguette.io", url="https://github.com/baguette-io/baguette-olite/", description="Python wrapper for gitolite", diff --git a/tests/test_group.py b/tests/test_group.py index 51a9f00..cc64f1a 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -89,7 +89,7 @@ def test_delete_not_exist(olite): def test_delete_in_repo(olite): """ - Delete a group which is in a repo. + When deleting a group, it should be removed from its repos. """ group = olite.groups.create('group1') repo = olite.repos.create('repo1') @@ -100,6 +100,22 @@ def test_delete_in_repo(olite): olite.groups.delete('group1') assert open(repo.config).read() == "repo repo1\n" +def test_delete_user_from_gitolite(olite, user_factory): + """ + When deleting an user, it should be removed from its groups. + """ + group = olite.groups.create('group1') + user_factory('user1') + user_factory('user2') + user_factory('user3') + olite.groups.user_add('group1', 'user1') + olite.groups.user_add('group1', 'user2') + olite.groups.user_add('group1', 'user3') + assert open(group.config).read() == "@group1 = user1\n@group1 = user2\n@group1 = user3\n" + # Delete user 2 + olite.users.delete('user2') + assert open(group.config).read() == "@group1 = user1\n@group1 = user3\n" + def test_add_user(olite, group1, user_factory): user_factory('user1') assert olite.groups.user_add('group1', 'user1') is True