11
11
12
12
13
13
def get_user_scope (user : User ):
14
+ """ Helper method to construct single user filebrowser scope.
15
+ Args:
16
+ user (User): The User model this action is for.
17
+
18
+ Returns:
19
+ path (string): The single user scope path relative to root filebrowser path.
20
+ """
14
21
return f"./users/{ user .username } "
15
22
16
23
17
24
def get_admin_login ():
25
+ """ Helper method to construct admin filebrowser login json string.
26
+
27
+ Returns:
28
+ admin_login (string): The admin login json string.
29
+ """
18
30
admin_login = {"username" : os .environ ["STORE_ADMIN_USERNAME" ],
19
31
"password" : os .environ ["STORE_ADMIN_PASSWORD" ]}
20
32
return admin_login
21
33
22
34
23
35
def get_user_login (user : User ):
36
+ """ Helper method to construct user.username's filebrowser login json string.
37
+
38
+ Args:
39
+ user (User): The User model this action is for.
40
+
41
+ Returns:
42
+ user_login (string): The user login json string.
43
+ """
24
44
if user .username == os .environ ["STORE_ADMIN_USERNAME" ]:
25
45
password = os .environ ["STORE_ADMIN_PASSWORD" ]
26
46
else :
@@ -31,6 +51,15 @@ def get_user_login(user: User):
31
51
32
52
33
53
def use_filestore_auth (user : User ):
54
+ """ Helper method of to login to user.username's filebrowser account.
55
+
56
+ Args:
57
+ user (User): The User model this action is for.
58
+
59
+ Returns:
60
+ fs_user_token (string): Updated filebrowser api jwt for user.username.
61
+ http_status (integer): HTTP status code from filebrowser api login.
62
+ """
34
63
if not user .is_authenticated :
35
64
return None
36
65
verify , host = get_rest_host ()
@@ -42,6 +71,17 @@ def use_filestore_auth(user: User):
42
71
43
72
44
73
def get_filestore_token (user_login , host , verify ):
74
+ """ Uses the filebrowser api to login the user.username's filebrowser account and return their auth jwt.
75
+
76
+ Args:
77
+ user_login (string): The user login json string.
78
+ host (string): The django runtime hostname.
79
+ verify (bool): True to verify the hostname tls certificate.
80
+
81
+ Returns:
82
+ fs_user_token (string): Updated filebrowser api jwt for user.username.
83
+ http_status (integer): HTTP status code from filebrowser api login.
84
+ """
45
85
try :
46
86
r_userlogin = requests .get (f"https://{ host } /storemng/api/login" ,
47
87
data = json .dumps (user_login ), verify = verify , timeout = FS_API_TIMEOUT )
@@ -53,22 +93,30 @@ def get_filestore_token(user_login, host, verify):
53
93
54
94
55
95
def add_filestore_auth (user : User ):
96
+ """ Uses the filebrowser api to add the user.username's filebrowser account and return their auth jwt.
97
+
98
+ Args:
99
+ user (User): The User model this action is for.
100
+
101
+ Returns:
102
+ fs_user_token (string): Updated filebrowser api jwt for user.username.
103
+ """
56
104
if not user .is_authenticated :
57
105
return None
58
106
verify , host = get_rest_host ()
59
107
# get auth for setting new user
60
108
admin_login = get_admin_login ()
61
109
admin_token , status = get_filestore_token (admin_login , host , verify )
62
110
if not admin_token :
63
- return False
111
+ return None
64
112
# get user defaults from global settings
65
113
try :
66
114
r_gset = requests .get (f"https://{ host } /storemng/api/settings" ,
67
115
headers = {"X-Auth" : admin_token }, verify = verify , timeout = FS_API_TIMEOUT )
68
116
r_gset .raise_for_status ()
69
117
except (requests .exceptions .ConnectionError , requests .exceptions .HTTPError ) as err :
70
118
print (err )
71
- return False
119
+ return None
72
120
settings = r_gset .json ()
73
121
# set new user options
74
122
fs_user = {
@@ -100,24 +148,32 @@ def add_filestore_auth(user: User):
100
148
101
149
102
150
def set_filestore_scope (user : User ):
151
+ """ Uses the filebrowser api to reset the user.username's filebrowser account scope and return their auth jwt.
152
+
153
+ Args:
154
+ user (User): The User model this action is for.
155
+
156
+ Returns:
157
+ fs_user_token (string): Updated filebrowser api jwt for user.username.
158
+ """
103
159
verify , host = get_rest_host ()
104
160
# get auth for setting new user
105
161
admin_login = get_admin_login ()
106
162
admin_token , status = get_filestore_token (admin_login , host , verify )
107
163
if not admin_token :
108
- return False
164
+ return None
109
165
# find user
110
166
fs_user_token , status = use_filestore_auth (user )
111
167
if not fs_user_token :
112
- return False
168
+ return None
113
169
payload = jwt .decode (fs_user_token , options = {"verify_signature" : False })
114
170
try :
115
171
r_user = requests .get (f"https://{ host } /storemng/api/users/{ payload ['user' ]['id' ]} " ,
116
172
headers = {"X-Auth" : admin_token }, verify = verify , timeout = FS_API_TIMEOUT )
117
173
r_user .raise_for_status ()
118
174
except (requests .exceptions .ConnectionError , requests .exceptions .HTTPError ) as err :
119
175
print (err )
120
- return False
176
+ return None
121
177
edit_user = r_user .json ()
122
178
if user .is_staff : # admin and staff get root scope
123
179
scope = "."
@@ -136,23 +192,31 @@ def set_filestore_scope(user: User):
136
192
r_useradd .raise_for_status ()
137
193
except (requests .exceptions .ConnectionError , requests .exceptions .HTTPError ) as err :
138
194
print (err )
139
- return False
195
+ return None
140
196
141
197
fs_user_token , status = use_filestore_auth (user )
142
198
return fs_user_token
143
199
144
200
145
201
def set_filestore_pass (user : User ):
202
+ """ Uses the filebrowser api to reset the user.username's filebrowser account password and return their auth jwt.
203
+
204
+ Args:
205
+ user (User): The User model this action is for.
206
+
207
+ Returns:
208
+ fs_user_token (string): Updated filebrowser api jwt for user.username.
209
+ """
146
210
if not user .is_authenticated :
147
- return False
211
+ return None
148
212
if user .username == os .environ ["STORE_ADMIN_USERNAME" ]:
149
- return False # root admin not allowed pass renew
213
+ return None # root admin not allowed pass renew
150
214
verify , host = get_rest_host ()
151
215
# get auth for removing user
152
216
admin_login = get_admin_login ()
153
217
admin_token , status = get_filestore_token (admin_login , host , verify )
154
218
if not admin_token :
155
- return False
219
+ return None
156
220
# find user without valid pass, loop through all
157
221
edit_user = {}
158
222
try :
@@ -188,6 +252,14 @@ def set_filestore_pass(user: User):
188
252
189
253
190
254
def delete_filestore_user (user : User ):
255
+ """ Uses the filebrowser api to delete the user.username's filebrowser account and files.
256
+
257
+ Args:
258
+ user (User): The User model this action is for.
259
+
260
+ Returns:
261
+ bool: True when user.username's filebrowser account and files are both removed.
262
+ """
191
263
if not user .is_authenticated :
192
264
return False
193
265
if user .username == os .environ ["STORE_ADMIN_USERNAME" ]:
0 commit comments