forked from snap-cloud/snapcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroles_controller.rb
More file actions
156 lines (132 loc) · 5.28 KB
/
roles_controller.rb
File metadata and controls
156 lines (132 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# frozen_string_literal: true
module Admin
class RolesController < Admin::BaseController
load_and_authorize_resource :conference, find_by: :short_title
before_action :set_selection
authorize_resource :role, except: :index
# Show flash message with ajax calls
after_action :prepare_unobtrusive_flash, only: %i[toggle_user toggle_comment_notifications]
def index
@roles = Role.where(resource: @conference)
tracks = @conference.program.tracks.where.not(submitter: nil)
@roles += Role.where(resource: tracks)
authorize! :index, @role
end
def show
@url = if @track
toggle_user_admin_conference_program_track_role_path(@conference.short_title, @track, @role.name)
else
toggle_user_admin_conference_role_path(@conference.short_title, @role.name)
end
@users_roles = UsersRole.where(role: @role).includes(:user)
@comment_notifications_url =
if @track.nil?
toggle_comment_notifications_admin_conference_role_path(@conference.short_title, @role.name)
end
end
def edit
@url = if @track
admin_conference_program_track_role_path(@conference.short_title, @track, @role.name)
else
admin_conference_role_path(@conference.short_title, @role.name)
end
@users = @role.users
end
def update
role_name = @role.name
if @role.update(role_params)
url = if @track
admin_conference_program_track_role_path(@conference.short_title, @track, @role.name)
else
admin_conference_role_path(@conference.short_title, @role.name)
end
redirect_to url,
notice: 'Successfully updated role ' + @role.name
else
@role.name = role_name
flash.now[: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]
url = if @track
admin_conference_program_track_role_path(@conference.short_title, @track, @role.name)
else
admin_conference_role_path(@conference.short_title, @role.name)
end
unless user
redirect_to url,
error: 'Could not find user. Please provide a valid email!'
return
end
# The conference must have at least 1 organizer
if @role.name == 'organizer' && state == 'false' && @role.users.count == 1
redirect_to admin_conference_role_path(@conference.short_title, @role.name),
error: 'The conference must have at least 1 organizer!'
return
end
if @role.resource_type == 'Conference'
role_resource = @conference
elsif @role.resource_type == 'Track'
role_resource = @track
end
# Remove user
if state == 'false'
if user.remove_role @role.name, role_resource
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_cached_role? @role.name, role_resource
flash[:error] = "User #{user.email} already has the role #{@role.name}"
# Add user
elsif user.add_role @role.name, role_resource
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 url }
end
end
def toggle_comment_notifications
user = User.find_by(email: user_params[:email])
state = user_params[:state]
redirect_url = admin_conference_role_path(@conference.short_title, @role.name)
unless user
redirect_to redirect_url, error: 'Could not find user. Please provide a valid email!' and return
end
users_role = UsersRole.find_by(user: user, role: @role)
unless users_role
redirect_to redirect_url, error: 'Could not find organizer setting for this user.' and return
end
# Be tolerant to different representations coming from the client (e.g. "true", "1", true).
email_notifications = ActiveModel::Type::Boolean.new.cast(state)
users_role.update!(email_notifications: email_notifications)
respond_to do |format|
format.js
format.html { redirect_to redirect_url, notice: 'Successfully updated notification setting.' }
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'
if @selection == 'track_organizer'
@track = @conference.program.tracks.find_by(short_name: params[:track_id])
@role = Role.find_by(name: @selection, resource: @track)
else
@role = Role.find_by(name: @selection, resource: @conference)
end
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