Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ssoauth
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
django
ssoauth
Commits
14c5d9ff
Commit
14c5d9ff
authored
3 years ago
by
Art
Browse files
Options
Downloads
Patches
Plain Diff
Better documentation and error handling when setting up predefined groups and permissions
parent
23e86d6f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ssoauth/management/commands/ssoauth_setup_groups_and_perms.py
+19
-8
19 additions, 8 deletions
...uth/management/commands/ssoauth_setup_groups_and_perms.py
with
19 additions
and
8 deletions
ssoauth/management/commands/ssoauth_setup_groups_and_perms.py
+
19
−
8
View file @
14c5d9ff
...
...
@@ -16,16 +16,23 @@ def get_or_create_permission(codename, create_with_name=None):
:return: permission object
Ensures the permissions exists. Creates it if needed.
"""
try
:
perm
=
Permission
.
objects
.
get
(
codename
=
codename
)
except
Permission
.
DoesNotExist
:
# the intended content type of the existing permission is not known
# therefore there might me multiple permissions with this codename
matching_perms
=
list
(
Permission
.
objects
.
filter
(
codename
=
codename
))
if
len
(
matching_perms
)
==
0
:
perm
=
Permission
.
objects
.
create
(
codename
=
codename
,
name
=
create_with_name
or
codename
,
content_type
=
get_user_content_type
()
content_type
=
get_user_content_type
()
# user model as fallback, also it fits perfectly for staff/superuser
)
logger
.
info
(
"
Created permission: {0}
"
.
format
(
perm
))
return
perm
elif
len
(
matching_perms
)
==
1
:
return
matching_perms
[
0
]
else
:
logger
.
warning
(
"
There are multiple permissions with codename {0}, picking the most recent one
"
.
format
(
codename
))
logger
.
warning
(
"
You probably need to cleanup the permissions for your project.
"
)
return
matching_perms
[
-
1
]
def
get_or_create_group
(
name
):
...
...
@@ -36,9 +43,9 @@ def get_or_create_group(name):
def
setup_predefined_groups
():
for
group_name
,
perm_codename
in
app_settings
.
PREDEFINED_GROUPS
.
items
():
for
group_name
,
perm_codename
s
in
app_settings
.
PREDEFINED_GROUPS
.
items
():
group
=
get_or_create_group
(
group_name
)
for
perm_codename
in
perm_codename
:
for
perm_codename
in
perm_codename
s
:
perm
=
get_or_create_permission
(
perm_codename
)
if
perm
not
in
group
.
permissions
.
all
():
group
.
permissions
.
add
(
perm
)
...
...
@@ -46,7 +53,11 @@ def setup_predefined_groups():
class
Command
(
BaseCommand
):
help
=
"
Set up groups and permissions (both predefined and compatibility). Runs automatically after migrations.
"
help
=
"""
Set up groups and permissions (both predefined and compatibility).
Runs automatically after migrations.
If running it manually (better don
'
t), ensure that all referenced permissions are created in prior.
"""
requires_migrations_checks
=
True
requires_system_checks
=
True
...
...
This diff is collapsed.
Click to expand it.
Art
@lukyanch
mentioned in commit
92815998
·
3 years ago
mentioned in commit
92815998
mentioned in commit 92815998b792e0fbf6c024a45369c3c8ba15a0ab
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment