-
Notifications
You must be signed in to change notification settings - Fork 4
Rôles multiples #4147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
arnaudlevy
wants to merge
15
commits into
main
Choose a base branch
from
multiple-roles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+817
−152
Draft
Rôles multiples #4147
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0470a27
Refonte
arnaudlevy 2942cc1
Marquage des endroits qui s'appuient sur le rôle unique
arnaudlevy 0becaaa
Update app/assets/javascripts/admin/commons/users.js
arnaudlevy b7ff23c
clean js
arnaudlevy 936723c
refactor
arnaudlevy 9151013
minitest
arnaudlevy b95d99c
Merge branch 'main' into multiple-roles
arnaudlevy 2aee6c5
User::Ability
arnaudlevy 46812e2
Merge branch 'main' into multiple-roles
arnaudlevy e722939
passage à un boolean (wip, le formulaire n'est pas encore correct)
arnaudlevy eb27974
role wip
arnaudlevy c9113db
Merge branch 'main' into multiple-roles
arnaudlevy 0615d45
Merge branch 'main' into multiple-roles
arnaudlevy aa71939
réparations
arnaudlevy fc93e07
Remove visitor
arnaudlevy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,91 @@ | ||
| /*global $ */ | ||
| $(function () { | ||
| 'use strict'; | ||
| // Affiche la cible d'un rôle et n'y propose que les options du bon type. | ||
| // Le type attendu est porté par l'option de rôle sélectionnée | ||
| // (data-scope-type, vide = rôle global) ; chaque option de cible porte aussi | ||
| // son type. Aucun mapping en dur : tout vient du HTML rendu. | ||
| window.osuny.userRoles = { | ||
| init: function () { | ||
| 'use strict'; | ||
| var rows, | ||
| i; | ||
| if (!document.body.matches('.users-edit, .users-new, .users-update, .users-create')) { | ||
| return; | ||
| } | ||
| rows = document.querySelectorAll('[data-user-role]'); | ||
| for (i = 0; i < rows.length; i += 1) { | ||
| this.refreshRow(rows[i]); | ||
| } | ||
| this.initEvents(); | ||
| }, | ||
|
|
||
| initEvents: function () { | ||
| 'use strict'; | ||
| document.addEventListener('change', this.onChangeRole.bind(this)); | ||
| $('#user_roles').on('cocoon:after-insert', this.onAfterInsert.bind(this)); | ||
| }, | ||
|
|
||
| onChangeRole: function (event) { | ||
| 'use strict'; | ||
| var roleSelect = event.target.closest('[data-user-role-role]'); | ||
| if (roleSelect !== null) { | ||
| this.refreshRow(roleSelect.closest('[data-user-role]')); | ||
| } | ||
| }, | ||
|
|
||
| onAfterInsert: function (event, row) { | ||
| 'use strict'; | ||
| this.refreshRow(row[0]); | ||
| }, | ||
|
|
||
| refreshRow: function (row) { | ||
| 'use strict'; | ||
| var roleSelect = row.querySelector('[data-user-role-role]'), | ||
| roleOption = roleSelect && roleSelect.options[roleSelect.selectedIndex], | ||
| type = roleOption && roleOption.getAttribute('data-scope-type'), | ||
| container = row.querySelector('[data-user-role-scope-container]'), | ||
| scope = row.querySelector('[data-user-role-scope]'); | ||
|
|
||
| var changeRole = function () { | ||
| var value = $('select[name="user[role]"]').val(), | ||
| showForRoles, | ||
| required; | ||
|
|
||
| $('*[data-show-for-roles]').each(function () { | ||
| showForRoles = $(this) | ||
| .attr('data-show-for-roles') | ||
| .split(','); | ||
| if ($.inArray(value, showForRoles) > -1) { | ||
| required = $(this).attr('data-required'); | ||
| if (required) { | ||
| $('input, select', this).attr('required', 'required'); | ||
| } else { | ||
| $('input, select', this).removeAttr('required'); | ||
| } | ||
| $(this).show(); | ||
| } else { | ||
| $(this).hide(); | ||
| // hidden field cannot be required | ||
| $('input, select', this).removeAttr('required'); | ||
| if (!type) { | ||
| container.classList.add('d-none'); | ||
| return; | ||
| } | ||
| container.classList.remove('d-none'); | ||
| this.filterScopeOptions(scope, type); | ||
| }, | ||
|
|
||
| // N'affiche que les cibles du bon type et, si la cible courante n'est pas | ||
| // du bon type, sélectionne la première valide. | ||
| filterScopeOptions: function (scope, type) { | ||
| 'use strict'; | ||
| var options = scope.options, | ||
| firstMatch = null, | ||
| selected = scope.options[scope.selectedIndex], | ||
| matches, | ||
| i; | ||
|
|
||
| for (i = 0; i < options.length; i += 1) { | ||
| matches = options[i].getAttribute('data-scope-type') === type; | ||
| options[i].hidden = !matches; | ||
| options[i].disabled = !matches; | ||
| if (matches && firstMatch === null) { | ||
| firstMatch = options[i].value; | ||
| } | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| if (!selected || selected.getAttribute('data-scope-type') !== type) { | ||
| scope.value = firstMatch; | ||
| } | ||
|
qltysh[bot] marked this conversation as resolved.
|
||
| }, | ||
|
|
||
| if ($('body').is('.users-edit, .users-new, .users-update, .users-create')) { | ||
| changeRole(); | ||
| $('select[name="user[role]"]').change(changeRole); | ||
| invoke: function () { | ||
| 'use strict'; | ||
| return { | ||
| init: this.init.bind(this) | ||
| }; | ||
| } | ||
| }.invoke(); | ||
|
|
||
| window.addEventListener('DOMContentLoaded', function () { | ||
| 'use strict'; | ||
| window.osuny.userRoles.init(); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class User::Ability | ||
| include CanCan::Ability | ||
|
|
||
| attr_reader :user | ||
|
|
||
| def self.for(user) | ||
| user ||= User.new # guest user (not logged in) | ||
| new(user) | ||
| end | ||
|
|
||
| def initialize(user) | ||
| @user = user ||= User.new # guest user (not logged in) | ||
| if user.server_admin? | ||
| can :manage, :all | ||
| else | ||
| # Les rôles sont fusionnés du moins au plus privilégié (cf. User#ability_roles) | ||
| # pour que, en cas de conflit, la règle du rôle le plus puissant — déclarée | ||
| # en dernier — l'emporte (CanCanCan : last rule wins). | ||
| user.ability_roles.each do |role_name| | ||
| merge "User::Ability::#{role_name.classify}".constantize.new(user) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| # Ids des cibles attachées à un rôle donné de l'utilisateur. | ||
| # Chaque ability scope ses règles aux cibles de SON rôle (cf. User#scopes_for), | ||
| # ce qui permet d'être par exemple author du site A et website_manager du site B | ||
| # sans que les périmètres ne débordent l'un sur l'autre. | ||
| def scoped_ids(role_name) | ||
| @user.scopes_for(role_name).map(&:id) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Ability::Admin < Ability | ||
| class User::Ability::Admin < User::Ability | ||
|
|
||
| def initialize(user) | ||
| super | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
app/models/ability/alumni_manager.rb → app/models/user/ability/alumni_manager.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/models/ability/server_admin.rb → app/models/user/ability/server_admin.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/models/ability/teacher.rb → app/models/user/ability/teacher.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| class Ability::Teacher < Ability | ||
| class User::Ability::Teacher < User::Ability | ||
|
|
||
| def initialize(user) | ||
| super | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.