forked from pupilfirst/pupilfirst
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add admin features for user management and Discord role assignment (p…
…upilfirst#1627) - Implements user viewing and editing capabilities for admins - Adds functionality to assign Discord roles to users - Introduces a feature to manage Discord roles --------- Co-authored-by: Raj Suhail <[email protected]> Co-authored-by: Hari Gopal <[email protected]>
- Loading branch information
1 parent
403333d
commit 050625d
Showing
52 changed files
with
2,128 additions
and
108 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
File renamed without changes
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,102 @@ | ||
module Schools | ||
class UsersController < ApplicationController | ||
layout "school" | ||
|
||
before_action :set_user, except: :index | ||
after_action :verify_authorized, except: :index | ||
after_action :verify_policy_scoped, only: :index | ||
|
||
# GET school/users | ||
def index | ||
authorize([:schools, current_user]) | ||
@users = policy_scope([:schools, User]) | ||
|
||
@presenter = Schools::Users::IndexPresenter.new(view_context, @users) | ||
end | ||
|
||
# GET school/users/:id | ||
def show | ||
authorize([:schools, @user]) | ||
|
||
@presenter = Schools::Users::ShowPresenter.new(view_context, @user) | ||
end | ||
|
||
# GET school/users/:id/edit | ||
def edit | ||
authorize([:schools, @user]) | ||
|
||
cohort_roles = | ||
@user.cohorts.map { |c| { name: c.name, role_ids: c.discord_role_ids } } | ||
fixed_role_ids = cohort_roles.flat_map { |c| c[:role_ids] } | ||
|
||
@fixed_roles = | ||
current_school | ||
.discord_roles | ||
.where(discord_id: fixed_role_ids) | ||
.map do |role| | ||
cohort_name = | ||
cohort_roles.find do |cr| | ||
cr[:role_ids].include?(role.discord_id) | ||
end[ | ||
:name | ||
] | ||
OpenStruct.new( | ||
cohort_name: cohort_name, | ||
role_name: role.name, | ||
role_color: role.color_hex | ||
) | ||
end | ||
|
||
@discord_roles = | ||
current_school.discord_roles.where.not(discord_id: fixed_role_ids) | ||
|
||
@user_roles = @user.discord_roles.where.not(discord_id: fixed_role_ids) | ||
end | ||
|
||
# PATCH /school/users/:id | ||
def update | ||
authorize([:schools, @user]) | ||
|
||
unless Schools::Configuration::Discord.new(current_school).configured? | ||
flash[:error] = t(".add_discord_config") | ||
|
||
redirect_to edit_school_user_path(@user) | ||
return | ||
end | ||
|
||
if @user.discord_user_id.blank? | ||
flash[:error] = t(".user_has_not_connected_discord") | ||
redirect_to school_user_path(@user) | ||
return | ||
end | ||
|
||
role_params = params.require(:user).permit(discord_role_ids: []) | ||
|
||
sync_service = | ||
Discord::SyncProfileService.new( | ||
@user, | ||
additional_discord_role_ids: role_params[:discord_role_ids] | ||
) | ||
|
||
sync_service.execute | ||
|
||
if sync_service.warning_msg.blank? | ||
flash[:success] = t(".successfully_synced_roles") | ||
elsif sync_service.warning_msg.present? | ||
flash[:warning] = sync_service.warning_msg | ||
end | ||
|
||
redirect_to school_user_path(@user) | ||
rescue Discord::SyncProfileService::SyncError => e | ||
flash[:error] = e.message | ||
|
||
redirect_to school_user_path(@user) | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = policy_scope([:schools, User]).find(params["id"]) | ||
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
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,62 @@ | ||
module Schools | ||
class DiscordConfigurationForm < Reform::Form | ||
attr_accessor :current_school | ||
|
||
property :server_id | ||
property :bot_user_id | ||
property :bot_token | ||
|
||
validate :validate_server_id | ||
validate :validate_bot_user_id | ||
validate :validate_bot_token | ||
|
||
def save | ||
config = current_school.configuration.fetch("discord", {}) | ||
|
||
config["server_id"] = server_id | ||
config["bot_user_id"] = bot_user_id | ||
config["bot_token"] = bot_token.presence || config.fetch("bot_token") | ||
|
||
current_school.update!( | ||
configuration: current_school.configuration.merge("discord" => config) | ||
) | ||
end | ||
|
||
private | ||
|
||
def validate_server_id | ||
return if server_id.blank? | ||
|
||
unless server_id.match?(/^\d+\z/) | ||
errors.add( | ||
:server_id, | ||
I18n.t("schools.discord_configuration_form.invalid_server_id") | ||
) | ||
end | ||
end | ||
|
||
def validate_bot_user_id | ||
return if bot_user_id.blank? | ||
|
||
unless bot_user_id.match?(/^\d+\z/) | ||
errors.add( | ||
:bot_user_id, | ||
I18n.t("schools.discord_configuration_form.invalid_bot_user_id") | ||
) | ||
end | ||
end | ||
|
||
def validate_bot_token | ||
return if bot_token.blank? | ||
|
||
unless bot_token.match?( | ||
/^[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+$/ | ||
) | ||
errors.add( | ||
:bot_token, | ||
I18n.t("schools.discord_configuration_form.invalid_bot_token") | ||
) | ||
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
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.