nextcloud.api_wrappers.user   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 56
dl 0
loc 167
rs 10
c 0
b 0
f 0

13 Methods

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