Passed
Push — master ( eeab6e...dd6af3 )
by Ramon
05:59
created

bika/lims/api/user.py (2 issues)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from AccessControl import getSecurityManager
22
from bika.lims.api import get_portal
23
from bika.lims.api import get_tool
24
from Products.CMFPlone.RegistrationTool import get_member_by_login_name
25
from Products.PlonePAS.tools.groupdata import GroupData
26
from Products.PlonePAS.tools.memberdata import MemberData
27
28
29
def get_user(user=None):
30
    """Get the user object
31
32
    :param user: A user id, memberdata object or None for the current user
33
    :returns: Plone User (PlonePAS) / Propertied User (PluggableAuthService)
34
    """
35
    if user is None:
36
        # Return the current authenticated user
37
        user = getSecurityManager().getUser()
38
    elif isinstance(user, MemberData):
39
        # MemberData wrapped user -> get the user object
40
        user = user.getUser()
41
    elif isinstance(user, basestring):
42
        # User ID -> get the user
43
        user = get_member_by_login_name(get_portal(), user, False)
44
        if user:
45
            user = user.getUser()
46
    return user
47
48
49
def get_user_id(user=None):
50
    """Get the user id of the current authenticated user
51
52
    :param user: A user id, memberdata object or None for the current user
53
    :returns: Plone user ID
54
    """
55
    user = get_user(user)
56
    if user is None:
57
        return None
58
    return user.getId()
59
60
61
def get_group(group):
62
    """Return the group
63
64
    :param group: The group name/id
65
    :returns: Group
66
    """
67
    portal_groups = get_tool("portal_groups")
68
    if isinstance(group, basestring):
69
        group = portal_groups.getGroupById(group)
70
    elif isinstance(group, GroupData):
71
        group = group
72
    return group
73
74
75
def get_groups(user=None):
76
    """Return the groups of the user
77
78
    :param user: A user id, memberdata object or None for the current user
79
    :returns: List of groups
80
    """
81
    portal_groups = get_tool("portal_groups")
82
    user = get_user(user)
83
    if user is None:
84
        return []
85
    return portal_groups.getGroupsForPrincipal(user)
86
87
88 View Code Duplication
def add_group(group, user=None):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
89
    """Add the user to the group
90
    """
91
    user = get_user(user)
92
93
    if user is None:
94
        raise ValueError("User '{}' not found".format(repr(user)))
95
96
    if isinstance(group, basestring):
97
        group = [group]
98
    elif isinstance(group, GroupData):
99
        group = [group]
100
101
    portal_groups = get_tool("portal_groups")
102
    for group in map(get_group, group):
103
        if group is None:
104
            continue
105
        portal_groups.addPrincipalToGroup(get_user_id(user), group.getId())
106
107
    return get_groups(user)
108
109
110 View Code Duplication
def del_group(group, user=None):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
111
    """Remove the user to the group
112
    """
113
    user = get_user(user)
114
115
    if user is None:
116
        raise ValueError("User '{}' not found".format(repr(user)))
117
118
    if isinstance(group, basestring):
119
        group = [group]
120
    elif isinstance(group, GroupData):
121
        group = [group]
122
123
    portal_groups = get_tool("portal_groups")
124
    for group in map(get_group, group):
125
        if group is None:
126
            continue
127
        portal_groups.removePrincipalFromGroup(
128
            get_user_id(user), group.getId())
129
130
    return get_groups(user)
131