Skip to content

Commit

Permalink
Merge pull request #806 from LukeTapekhin/heavy_refactor
Browse files Browse the repository at this point in the history
roles fixes
  • Loading branch information
LukeTapekhin authored Sep 4, 2017
2 parents 9fa8178 + 70f4180 commit 07213dc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
46 changes: 34 additions & 12 deletions lingvodoc/views/v2/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,41 @@ def edit_dictionary_roles(request): # tested & in docs
req = request.json_body

for role_name in req['roles_users']:
remove_list = list()
for user in req['roles_users'][role_name]:
if user in previous['roles_users'][role_name]:
previous['roles_users'][role_name].remove(user)
remove_list.append(user)
for user in remove_list:
req['roles_users'][role_name].remove(user)

for role_name in req['roles_organizations']:
remove_list = list()
for user in req['roles_organizations'][role_name]:
if user in previous['roles_organizations'][role_name]:
previous['roles_organizations'][role_name].remove(user)

subreq = Request.blank(url)
subreq.json = previous
subreq.method = 'PATCH'
headers = {'Cookie': request.headers['Cookie']}
subreq.headers = headers
request.invoke_subrequest(subreq)

req['roles_organizations'][role_name].remove(user)
for user in remove_list:
req['roles_users'][role_name].remove(user)

delete_flag = False

for role_name in previous['roles_users']:
if previous['roles_users'][role_name]:
delete_flag = True
break

for role_name in previous['roles_organizations']:
if previous['roles_organizations'][role_name]:
delete_flag = True
break
if delete_flag:
subreq = Request.blank(url)
subreq.json = previous
subreq.method = 'PATCH'
headers = {'Cookie': request.headers['Cookie']}
subreq.headers = headers
request.invoke_subrequest(subreq)
roles_users = None
if 'roles_users' in req:
roles_users = req['roles_users']
Expand Down Expand Up @@ -677,8 +696,9 @@ def edit_dictionary_roles(request): # tested & in docs
if user not in group.users:
group.users.append(user)
else:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}
if roles_users[role_name]:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}

if roles_organizations:
for role_name in roles_organizations:
Expand Down Expand Up @@ -717,8 +737,9 @@ def edit_dictionary_roles(request): # tested & in docs
if org not in group.organizations:
group.organizations.append(org)
else:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}
if roles_organizations[role_name]:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}

request.response.status = HTTPOk.code
return response
Expand All @@ -736,6 +757,7 @@ def delete_dictionary_roles(request): # & in docs
req = json.loads(request.json_body)
else:
req = request.json_body
print(req)

roles_users = None
if 'roles_users' in req:
Expand Down
45 changes: 35 additions & 10 deletions lingvodoc/views/v2/perspective/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,22 +1034,45 @@ def edit_perspective_roles(request):
else:
req = request.json_body


for role_name in req['roles_users']:
remove_list = list()
for user in req['roles_users'][role_name]:
if user in previous['roles_users'][role_name]:
previous['roles_users'][role_name].remove(user)
remove_list.append(user)
for user in remove_list:
req['roles_users'][role_name].remove(user)

for role_name in req['roles_organizations']:
remove_list = list()
for user in req['roles_organizations'][role_name]:
if user in previous['roles_organizations'][role_name]:
previous['roles_organizations'][role_name].remove(user)
req['roles_organizations'][role_name].remove(user)
for user in remove_list:
req['roles_users'][role_name].remove(user)

subreq = Request.blank(url)
subreq.json = previous
subreq.method = 'PATCH'
headers = {'Cookie': request.headers['Cookie']}
subreq.headers = headers
request.invoke_subrequest(subreq)

delete_flag = False

for role_name in previous['roles_users']:
if previous['roles_users'][role_name]:
delete_flag = True
break

for role_name in previous['roles_organizations']:
if previous['roles_organizations'][role_name]:
delete_flag = True
break

if delete_flag:
subreq = Request.blank(url)
subreq.json = previous
subreq.method = 'PATCH'
headers = {'Cookie': request.headers['Cookie']}
subreq.headers = headers
request.invoke_subrequest(subreq)

roles_users = None
if 'roles_users' in req:
Expand Down Expand Up @@ -1101,8 +1124,9 @@ def edit_perspective_roles(request):
if user not in group.users:
group.users.append(user)
else:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}
if roles_users[role_name]:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}

if roles_organizations:
for role_name in roles_organizations:
Expand Down Expand Up @@ -1140,8 +1164,9 @@ def edit_perspective_roles(request):
if org not in group.organizations:
group.organizations.append(org)
else:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}
if roles_organizations[role_name]:
request.response.status = HTTPForbidden.code
return {'error': str("Not enough permission")}

request.response.status = HTTPOk.code
return response
Expand Down

0 comments on commit 07213dc

Please sign in to comment.