-
-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3720 from rubyforgood/3554-superadmin-roles
3554 Allow superadmins to manage roles
- Loading branch information
Showing
16 changed files
with
473 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,34 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import $ from 'jquery'; | ||
import "select2" | ||
|
||
export default class extends Controller { | ||
static targets = ['source', 'destination'] | ||
static values = { | ||
url: String | ||
} | ||
|
||
sourceChanged() { | ||
const val = $(this.sourceTarget).val() | ||
const url = new URL(this.urlValue) | ||
url.searchParams.append('resource_type', val); | ||
$(this.destinationTarget).select2({ | ||
ajax: { | ||
url: url.toString(), | ||
dataType: 'json' | ||
} | ||
}); | ||
|
||
} | ||
|
||
connect() { | ||
/** | ||
* This is a workaround to auto focus on the select2 input when it is opened. | ||
*/ | ||
$(this.destinationTarget).on('select2:open', function (e) { | ||
$(".select2-search__field")[0].focus(); | ||
}) | ||
this.sourceChanged(); | ||
} | ||
|
||
} |
This file contains 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 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,29 @@ | ||
class AddRoleService | ||
# @param user_id [Integer] | ||
# @param resource_id [Integer] | ||
# @param resource_type [String] | ||
def self.call(user_id:, resource_type:, resource_id: nil) | ||
user = User.find(user_id) | ||
if resource_type.to_sym == Role::SUPER_ADMIN | ||
add_super_admin(user) | ||
return | ||
end | ||
klass = Role::TITLE_TO_RESOURCE[resource_type.to_sym] | ||
resource = klass.find(resource_id) | ||
if user.has_role?(resource_type, resource) | ||
raise "User #{user.name} already has role for #{resource.name}" | ||
end | ||
user.add_role(resource_type, resource) | ||
if resource_type.to_sym == Role::ORG_ADMIN | ||
user.add_role(:org_user, resource) | ||
end | ||
end | ||
|
||
# @param user [User] | ||
def self.add_super_admin(user) | ||
if user.has_role?(Role::SUPER_ADMIN) | ||
raise "User #{user.name} already has super admin role!" | ||
end | ||
user.add_role(Role::SUPER_ADMIN) | ||
end | ||
end |
This file contains 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,28 @@ | ||
class RemoveRoleService | ||
# @param user_id [Integer] | ||
# @param role_id [Integer] | ||
# @param resource_type [String] | ||
# @param resource_id [Integer] | ||
def self.call(user_id:, role_id: nil, resource_type: nil, resource_id: nil) | ||
if role_id.nil? && resource_id.nil? | ||
raise "Must provide either a role ID or resource ID!" | ||
end | ||
if role_id.nil? | ||
role_id = Role.find_by(name: resource_type, resource_id: resource_id).id | ||
end | ||
user_role = UsersRole.find_by(user_id: user_id, role_id: role_id) | ||
unless user_role | ||
user = User.find(user_id) | ||
role = Role.find(role_id) | ||
raise "User #{user.name} does not have role for #{role.resource.name}!" | ||
end | ||
|
||
user_role.destroy | ||
if user_role.role.name.to_sym == Role::ORG_USER # they can't be an admin if they're not a user | ||
admin_role = Role.find_by(resource_id: user_role.role.resource_id, name: Role::ORG_ADMIN) | ||
if admin_role | ||
UsersRole.find_by(user_id: user_id, role_id: admin_role.id)&.destroy | ||
end | ||
end | ||
end | ||
end |
This file contains 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,68 @@ | ||
<section class="content"> | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<!-- left column --> | ||
<div class="col-md-12"> | ||
<!-- jquery validation --> | ||
<div class="card card-primary card-outline"> | ||
<div class="card-header"> | ||
<h5 class="card-title"><%= user.name %> - Roles | ||
</h5> | ||
</div> | ||
<!-- /.card-header --> | ||
<!-- form start --> | ||
<div class="card-body"> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>Role Type</th> | ||
<th>Resource</th> | ||
<th class="text-right">Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% user.roles.each do |role| %> | ||
<tr> | ||
<td><%= role.title %></td> | ||
<td><%= link_to role.resource.name, role.resource %></td> | ||
<td class="text-right"> | ||
<%= delete_button_to admin_user_remove_role_path(user, role_id: role.id), | ||
confirm: "Are you sure you want to remove this role?" %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<div data-controller="double-select" data-double-select-url-value="<%= resource_ids_admin_users_url %>"> | ||
<h3>Add Role</h3> | ||
<%= form_tag admin_user_add_role_path(user) do %> | ||
<div class="form-inputs"> | ||
<div class="form-group"> | ||
<label>Type</label> | ||
<div class="input-group"> | ||
<%= select_tag :resource_type, options_for_select(@resources), | ||
class: 'select form-control', | ||
data: { 'double-select-target': 'source', | ||
'action': 'double-select#sourceChanged' | ||
} | ||
%> | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<label>Resource</label> | ||
<div class="input-group"> | ||
<%= select_tag :resource_id, [], class: 'form-control', data: { | ||
'double-select-target': 'destination' | ||
} %> | ||
</div> | ||
</div> | ||
</div> | ||
<%= submit_tag 'Add Role', class: 'btn btn-md btn-primary' %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</section> |
This file contains 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 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 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.