2323from gettext import gettext as _
2424from typing import Tuple
2525
26- from flask import abort , jsonify , render_template
26+ from flask import abort , current_app , jsonify , render_template
2727from flask_jwt_extended import create_access_token , get_jwt , get_jwt_identity
2828from webargs import fields
2929
5050 PERM_EDIT_OTHER_USER ,
5151 PERM_EDIT_OWN_USER ,
5252 PERM_EDIT_USER_ROLE ,
53+ PERM_EDIT_USER_TREE ,
5354 PERM_MAKE_ADMIN ,
5455 PERM_VIEW_OTHER_TREE_USER ,
5556 PERM_VIEW_OTHER_USER ,
6162 SCOPE_CREATE_ADMIN ,
6263 SCOPE_RESET_PW ,
6364)
65+ from ...const import TREE_MULTI
6466from ..auth import has_permissions , require_permissions
6567from ..ratelimiter import limiter
6668from ..tasks import (
7173 send_email_new_user ,
7274 send_email_reset_password ,
7375)
74- from ..util import get_tree_from_jwt , get_tree_id , use_args
76+ from ..util import get_tree_from_jwt , get_tree_id , tree_exists , use_args
7577from . import LimitedScopeProtectedResource , ProtectedResource , Resource
7678
7779
@@ -154,6 +156,7 @@ def get(self, user_name: str):
154156 "email" : fields .Str (required = False ),
155157 "full_name" : fields .Str (required = False ),
156158 "role" : fields .Int (required = False ),
159+ "tree" : fields .Str (required = False ),
157160 },
158161 location = "json" ,
159162 )
@@ -168,11 +171,16 @@ def put(self, args, user_name: str):
168171 require_permissions ([PERM_EDIT_OTHER_TREE_USER_ROLE ])
169172 else :
170173 require_permissions ([PERM_EDIT_USER_ROLE ])
174+ if "tree" in args :
175+ require_permissions ([PERM_EDIT_USER_TREE ])
176+ if not tree_exists (args ["tree" ]):
177+ abort (422 )
171178 modify_user (
172179 name = user_name ,
173180 email = args .get ("email" ),
174181 fullname = args .get ("full_name" ),
175182 role = args .get ("role" ),
183+ tree = args .get ("tree" ),
176184 )
177185 return "" , 200
178186
@@ -199,6 +207,8 @@ def post(self, args, user_name: str):
199207 require_permissions ([PERM_ADD_USER ])
200208 else :
201209 require_permissions ([PERM_ADD_OTHER_TREE_USER ])
210+ if not tree_exists (args ["tree" ]):
211+ abort (422 )
202212 try :
203213 add_user (
204214 name = user_name ,
@@ -250,9 +260,14 @@ def post(self, args, user_name: str):
250260 if user_name == "-" :
251261 # Registering a new user does not make sense for "own" user
252262 abort (404 )
263+ if not args .get ("tree" ) and current_app .config ["TREE" ] == TREE_MULTI :
264+ # if multi-tree is enabled, tree is required
265+ abort (422 )
253266 # do not allow registration if no tree owner account exists!
254267 if get_number_users (tree = args .get ("tree" ), roles = (ROLE_OWNER ,)) == 0 :
255268 abort (405 )
269+ if "tree" in args and not tree_exists (args ["tree" ]):
270+ abort (422 )
256271 try :
257272 add_user (
258273 name = user_name ,
@@ -292,17 +307,20 @@ class UserCreateOwnerResource(LimitedScopeProtectedResource):
292307 location = "json" ,
293308 )
294309 def post (self , args , user_name : str ):
295- """Create a user with owner permissions."""
310+ """Create a user with admin permissions."""
296311 if user_name == "-" :
297312 # User name - is not allowed
298313 abort (404 )
314+ # FIXME what about multi-tree
299315 if get_number_users () > 0 :
300316 # there is already a user in the user DB
301317 abort (405 )
302318 claims = get_jwt ()
303319 if claims [CLAIM_LIMITED_SCOPE ] != SCOPE_CREATE_ADMIN :
304320 # This is a wrong token!
305321 abort (403 )
322+ if "tree" in args and not tree_exists (args ["tree" ]):
323+ abort (422 )
306324 add_user (
307325 name = user_name ,
308326 password = args ["password" ],
0 commit comments