Skip to content

Commit

Permalink
When deleting an user, remove it from all the groups it belongs to
Browse files Browse the repository at this point in the history
  • Loading branch information
pydavid committed Jul 28, 2017
1 parent dd84c02 commit 030b599
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
7 changes: 5 additions & 2 deletions pyolite/managers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
30 changes: 18 additions & 12 deletions pyolite/models/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
from six import string_types
from unipath import Path
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 17 additions & 1 deletion tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit 030b599

Please sign in to comment.