forked from DGNum/gestioCOF
106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
|
from django.contrib.auth import get_user_model
|
||
|
from django.contrib.auth.models import Permission
|
||
|
|
||
|
from ..models import Account
|
||
|
|
||
|
|
||
|
User = get_user_model()
|
||
|
|
||
|
|
||
|
def user_add_perms(user, perms_labels):
|
||
|
"""
|
||
|
Add perms to a user.
|
||
|
|
||
|
Args:
|
||
|
user (User instance)
|
||
|
perms (list of str 'app.perm_name')
|
||
|
|
||
|
Returns:
|
||
|
The same user (refetched from DB to avoid missing perms)
|
||
|
|
||
|
"""
|
||
|
u_labels = set(perms_labels)
|
||
|
|
||
|
perms = []
|
||
|
for label in u_labels:
|
||
|
app_label, codename = label.split('.', 1)
|
||
|
perms.append(
|
||
|
Permission.objects.get(
|
||
|
content_type__app_label=app_label,
|
||
|
codename=codename,
|
||
|
)
|
||
|
)
|
||
|
|
||
|
user.user_permissions.add(*perms)
|
||
|
|
||
|
# If permissions have already been fetched for this user, we need to reload
|
||
|
# it to avoid using of the previous permissions cache.
|
||
|
# https://docs.djangoproject.com/en/1.11/topics/auth/default/#permission-caching
|
||
|
return User.objects.get(pk=user.pk)
|
||
|
|
||
|
|
||
|
def _create_user_and_account(user_attrs, account_attrs, perms=None):
|
||
|
user_attrs.setdefault('password', user_attrs['username'])
|
||
|
user = User.objects.create_user(**user_attrs)
|
||
|
|
||
|
account_attrs['cofprofile'] = user.profile
|
||
|
kfet_pwd = account_attrs.pop('password', None)
|
||
|
|
||
|
account = Account.objects.create(**account_attrs)
|
||
|
|
||
|
if perms is not None:
|
||
|
user = user_add_perms(user, perms)
|
||
|
|
||
|
if 'kfet.is_team' in perms:
|
||
|
if kfet_pwd is None:
|
||
|
kfet_pwd = 'kfetpwd_{}'.format(user_attrs['password'])
|
||
|
account.change_pwd(kfet_pwd)
|
||
|
account.save()
|
||
|
|
||
|
return user
|
||
|
|
||
|
|
||
|
def create_user(username='user', trigramme='000', **kwargs):
|
||
|
user_attrs = kwargs.setdefault('user_attrs', {})
|
||
|
|
||
|
user_attrs.setdefault('username', username)
|
||
|
user_attrs.setdefault('first_name', 'first')
|
||
|
user_attrs.setdefault('last_name', 'last')
|
||
|
user_attrs.setdefault('email', 'mail@user.net')
|
||
|
|
||
|
account_attrs = kwargs.setdefault('account_attrs', {})
|
||
|
account_attrs.setdefault('trigramme', trigramme)
|
||
|
|
||
|
return _create_user_and_account(**kwargs)
|
||
|
|
||
|
|
||
|
def create_team(username='team', trigramme='100', **kwargs):
|
||
|
user_attrs = kwargs.setdefault('user_attrs', {})
|
||
|
|
||
|
user_attrs.setdefault('username', username)
|
||
|
user_attrs.setdefault('first_name', 'team')
|
||
|
user_attrs.setdefault('last_name', 'member')
|
||
|
user_attrs.setdefault('email', 'mail@team.net')
|
||
|
|
||
|
account_attrs = kwargs.setdefault('account_attrs', {})
|
||
|
account_attrs.setdefault('trigramme', trigramme)
|
||
|
|
||
|
perms = kwargs.setdefault('perms', [])
|
||
|
perms.append('kfet.is_team')
|
||
|
|
||
|
return _create_user_and_account(**kwargs)
|
||
|
|
||
|
|
||
|
def create_root(username='root', trigramme='200', **kwargs):
|
||
|
user_attrs = kwargs.setdefault('user_attrs', {})
|
||
|
|
||
|
user_attrs.setdefault('username', username)
|
||
|
user_attrs.setdefault('first_name', 'super')
|
||
|
user_attrs.setdefault('last_name', 'user')
|
||
|
user_attrs.setdefault('email', 'mail@root.net')
|
||
|
|
||
|
account_attrs = kwargs.setdefault('account_attrs', {})
|
||
|
account_attrs.setdefault('trigramme', trigramme)
|
||
|
|
||
|
return _create_user_and_account(**kwargs)
|