mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
Roles into their own controller. Rework interface. Add flash messages to ajax calls. Add description to roles. Possible to edit roles in use.
This commit is contained in:
parent
eda5c2bc07
commit
ccd7ecbb90
59 changed files with 618 additions and 356 deletions
87
app/controllers/admin/roles_controller.rb
Normal file
87
app/controllers/admin/roles_controller.rb
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
module Admin
|
||||
class RolesController < Admin::BaseController
|
||||
load_and_authorize_resource :conference, find_by: :short_title
|
||||
before_action :set_selection
|
||||
# Show flash message with ajax calls
|
||||
after_action :prepare_unobtrusive_flash, only: :toggle_user
|
||||
|
||||
def index
|
||||
@roles = Role.where(resource: @conference)
|
||||
end
|
||||
|
||||
def show
|
||||
@users = @role.users
|
||||
end
|
||||
|
||||
def edit
|
||||
@users = @role.users
|
||||
end
|
||||
|
||||
def update
|
||||
role_name = @role.name
|
||||
|
||||
if @role.update_attributes(role_params)
|
||||
flash[:notice] = 'Successfully updated role ' + @role.name
|
||||
redirect_to admin_conference_role_path(@conference.short_title, @role.name)
|
||||
else
|
||||
@role.name = role_name
|
||||
flash[:error] = 'Could not update role! ' + @role.errors.full_messages.to_sentence
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def toggle_user
|
||||
user = User.find_by(email: user_params[:email])
|
||||
state = user_params[:state]
|
||||
|
||||
unless user
|
||||
flash[:error] = 'Could not find user. Please provide a valid email!'
|
||||
redirect_to(admin_conference_role_path(@conference.short_title, @role.name)) && return
|
||||
end
|
||||
|
||||
# The conference must have at least 1 organizer
|
||||
if @role.name == 'organizer' && state == 'false' && @role.users.count == 1
|
||||
flash[:error] = 'The conference must have at least 1 organizer!'
|
||||
redirect_to(admin_conference_role_path(@conference.short_title, @role.name)) && return
|
||||
end
|
||||
|
||||
# Remove user
|
||||
if state == 'false'
|
||||
if user.remove_role @role.name, @conference
|
||||
flash[:notice] = "Successfully removed role #{@role.name} from user #{user.email}"
|
||||
else
|
||||
flash[:error] = "Could not remove role #{@role.name} from user #{user.email}"
|
||||
end
|
||||
elsif user.has_role? @role.name, @conference
|
||||
flash[:error] = "User #{user.email} already has the role #{@role.name}"
|
||||
# Add user
|
||||
elsif user.add_role @role.name, @conference
|
||||
flash[:notice] = "Successfully added role #{@role.name} to user #{user.email}"
|
||||
else
|
||||
flash[:error] = "Coud not add role #{@role.name} to #{user.email}"
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.js
|
||||
format.html { redirect_to admin_conference_role_path(@conference.short_title, @role.name) }
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def set_selection
|
||||
# Set 'organizer' as default role, when there is no other selection
|
||||
@selection = params[:id] ? params[:id].parameterize.underscore : 'organizer'
|
||||
|
||||
@role = Role.find_by(name: @selection, resource: @conference)
|
||||
end
|
||||
|
||||
def role_params
|
||||
params.require(:role).permit(:name, :description, user_ids: [])
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(:email, :state)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue