Completed
Push — master ( a9b8b6...971575 )
by Matěj
12s
created

nextcloud.api_wrappers.user.User.create_subadmin()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
from nextcloud.base import WithRequester
2
3
4
class User(WithRequester):
5
    API_URL = "/ocs/v1.php/cloud/users"
6
    SUCCESS_CODE = 100
7
8
    def add_user(self, uid, passwd):
9
        """
10
        Create a new user on the Nextcloud server
11
12
        :param uid: str, uid of new user
13
        :param passwd: str, password of new user
14
        :return:
15
        """
16
        msg = {'userid': uid, 'password': passwd}
17
        return self.requester.post("", msg)
18
19
    def get_users(self, search=None, limit=None, offset=None):
20
        """
21
        Retrieve a list of users from the Nextcloud server
22
23
        :param search: string, optional search string
24
        :param limit: int, optional limit value
25
        :param offset: int, optional offset value
26
        :return:
27
        """
28
        params = {
29
            'search': search,
30
            'limit': limit,
31
            'offset': offset
32
        }
33
        return self.requester.get(params=params)
34
35
    def get_user(self, uid):
36
        """
37
        Retrieve information about a single user
38
39
        :param uid: str, uid of user
40
        :return:
41
        """
42
        return self.requester.get("{uid}".format(uid=uid))
43
44
    def edit_user(self, uid, what, value):
45
        """
46
        Edit attributes related to a user
47
48
        Users are able to edit email, displayname and password; admins can also edit the quota value
49
50
        :param uid: str, uid of user
51
        :param what: str, the field to edit
52
        :param value: str, the new value for the field
53
        :return:
54
        """
55
        what_to_key_map = dict(
56
            email="email", quota="quote", phone="phone", address="address", website="website",
57
            twitter="twitter", displayname="displayname", password="password",
58
        )
59
        assert what in what_to_key_map, (
60
            "You have chosen to edit user's '{what}', but you can choose only from: {choices}"
61
                .format(what=what, choices=", ".join(what_to_key_map.keys()))
62
        )
63
64
        url = "{uid}".format(uid=uid)
65
        msg = dict(
66
            key=what_to_key_map[what],
67
            value=value,
68
        )
69
        return self.requester.put(url, msg)
70
71
    def disable_user(self, uid):
72
        """
73
        Disable a user on the Nextcloud server so that the user cannot login anymore
74
75
        :param uid: str, uid of user
76
        :return:
77
        """
78
        return self.requester.put("{uid}/disable".format(uid=uid))
79
80
    def enable_user(self, uid):
81
        """
82
        Enable a user on the Nextcloud server so that the user can login again
83
84
        :param uid: str, uid of user
85
        :return:
86
        """
87
        return self.requester.put("{uid}/enable".format(uid=uid))
88
89
    def delete_user(self, uid):
90
        """
91
        Delete a user from the Nextcloud server
92
93
        :param uid: str, uid of user
94
        :return:
95
        """
96
        return self.requester.delete("{uid}".format(uid=uid))
97
98
    def add_to_group(self, uid, gid):
99
        """
100
        Add the specified user to the specified group
101
102
        :param uid: str, uid of user
103
        :param gid: str, name of group
104
        :return:
105
        """
106
        url = "{uid}/groups".format(uid=uid)
107
        msg = {'groupid': gid}
108
        return self.requester.post(url, msg)
109
110
    def remove_from_group(self, uid, gid):
111
        """
112
        Remove the specified user from the specified group
113
114
        :param uid: str, uid of user
115
        :param gid: str, name of group
116
        :return:
117
        """
118
        url = "{uid}/groups".format(uid=uid)
119
        msg = {'groupid': gid}
120
        return self.requester.delete(url, msg)
121
122
    def create_subadmin(self, uid, gid):
123
        """
124
        Make a user the subadmin of a group
125
126
        :param uid: str, uid of user
127
        :param gid: str, name of group
128
        :return:
129
        """
130
        url = "{uid}/subadmins".format(uid=uid)
131
        msg = {'groupid': gid}
132
        return self.requester.post(url, msg)
133
134
    def remove_subadmin(self, uid, gid):
135
        """
136
        Remove the subadmin rights for the user specified from the group specified
137
138
        :param uid: str, uid of user
139
        :param gid: str, name of group
140
        :return:
141
        """
142
        url = "{uid}/subadmins".format(uid=uid)
143
        msg = {'groupid': gid}
144
        return self.requester.delete(url, msg)
145
146
    def get_subadmin_groups(self, uid):
147
        """
148
        Get the groups in which the user is a subadmin
149
150
        :param uid: str, uid of user
151
        :return:
152
        """
153
        url = "{uid}/subadmins".format(uid=uid)
154
        return self.requester.get(url)
155
156
    def resend_welcome_mail(self, uid):
157
        """
158
        Trigger the welcome email for this user again
159
160
        :param uid: str, uid of user
161
        :return:
162
        """
163
        url = "{uid}/welcome".format(uid=uid)
164
        return self.requester.post(url)
165