Module: Workbench::SessionsHelper
- Included in:
- ApplicationController
- Defined in:
- app/helpers/workbench/sessions_helper.rb
Overview
These are used in both controllers and views.
Instance Method Summary collapse
- #administers_projects? ⇒ Boolean
- #administration_link ⇒ Object
- #authorize_project_selection(user, project) ⇒ Object
-
#can_administer_projects? ⇒ Boolean
User is some project_administrator or administrator.
-
#favorite_page_link(kind, name) ⇒ Object
TODO: NOT here.
- #favorites?(kind, name) ⇒ Boolean
- #has_hub_favorites? ⇒ Boolean
-
#is_administrator? ⇒ Boolean
Authorization methods.
-
#is_project_administrator? ⇒ Boolean
Can be optimized to just look at ProjectMembers likely.
- #is_project_member?(user, project) ⇒ Boolean
- #is_project_member_by_id(user_id, project_id) ⇒ Object
-
#is_superuser? ⇒ Boolean
A superuser is an administrator or a person who is a project_administrator IN THE CURRENTLY SELECTED PROJECT.
- #project_settings_link ⇒ Object
- #require_administrator_sign_in ⇒ Object
- #require_project_administrator_sign_in ⇒ Object
- #require_project_selection ⇒ Object
- #require_sign_in ⇒ Object
- #require_sign_in_and_project_selection ⇒ Object
- #require_superuser_sign_in ⇒ Object
-
#session_header_links ⇒ Object
TODO: make this a non-controller method.
- #sessions_clear_selected_project ⇒ Object
- #sessions_current_project ⇒ Object
- #sessions_current_project_id ⇒ Object
- #sessions_current_project_id=(project_id) ⇒ Object
- #sessions_current_user ⇒ Object (also: #current_user)
- #sessions_current_user=(user) ⇒ Object
- #sessions_current_user_id ⇒ Object
- #sessions_project_selected? ⇒ Boolean
- #sessions_select_project(project) ⇒ Object
- #sessions_sign_in(user, request) ⇒ Object
- #sessions_sign_out ⇒ Object
-
#sessions_signed_in? ⇒ Boolean
User methods.
-
#set_project_from_params ⇒ Object
Project methods.
Instance Method Details
#administers_projects? ⇒ Boolean
121 122 123 |
# File 'app/helpers/workbench/sessions_helper.rb', line 121 def administers_projects? sessions_signed_in? && (is_administrator? || sessions_current_user.administers_projects? ) end |
#administration_link ⇒ Object
215 216 217 |
# File 'app/helpers/workbench/sessions_helper.rb', line 215 def administration_link sessions_current_user.is_administrator? ? link_to('Administration', administration_path) : nil end |
#authorize_project_selection(user, project) ⇒ Object
138 139 140 |
# File 'app/helpers/workbench/sessions_helper.rb', line 138 def (user, project) project.project_members.where(user:, project:) end |
#can_administer_projects? ⇒ Boolean
User is some project_administrator or administrator
173 174 175 |
# File 'app/helpers/workbench/sessions_helper.rb', line 173 def can_administer_projects? redirect_to root_url, notice: 'Please sign in as a project administrator or administrator.' unless administers_projects? end |
#favorite_page_link(kind, name) ⇒ Object
TODO: NOT here
193 194 195 196 197 198 199 |
# File 'app/helpers/workbench/sessions_helper.rb', line 193 def favorite_page_link(kind, name) if favorites?(kind, name) link_to('Unfavorite page', unfavorite_page_path(kind:, name:), method: :post, remote: true, id: "unfavorite_link_#{kind}-#{name}", class: :unfavorite_link, title: 'Remove to favorite') else link_to('Favorite page', favorite_page_path(kind:, name:), method: :post, remote: true, id: "favorite_link_#{kind}-#{name}", class: :favourite_link, title: 'Add to favorite.') end end |
#favorites?(kind, name) ⇒ Boolean
207 208 209 |
# File 'app/helpers/workbench/sessions_helper.rb', line 207 def favorites?(kind, name) has_hub_favorites? && sessions_current_user.hub_favorites[sessions_current_project_id.to_s][kind].include?(name) end |
#has_hub_favorites? ⇒ Boolean
201 202 203 |
# File 'app/helpers/workbench/sessions_helper.rb', line 201 def has_hub_favorites? sessions_current_user.hub_favorites[sessions_current_project_id.to_s] ? true : false end |
#is_administrator? ⇒ Boolean
Authorization methods
111 112 113 |
# File 'app/helpers/workbench/sessions_helper.rb', line 111 def is_administrator? sessions_signed_in? && sessions_current_user.is_administrator? end |
#is_project_administrator? ⇒ Boolean
Can be optimized to just look at ProjectMembers likely
116 117 118 119 |
# File 'app/helpers/workbench/sessions_helper.rb', line 116 def is_project_administrator? sessions_signed_in? && sessions_project_selected? && sessions_current_project.project_members.exists?(is_project_administrator: true, user_id: sessions_current_user_id) end |
#is_project_member?(user, project) ⇒ Boolean
130 131 132 |
# File 'app/helpers/workbench/sessions_helper.rb', line 130 def is_project_member?(user, project) project.project_members.include?(user) # TODO - change to ID end |
#is_project_member_by_id(user_id, project_id) ⇒ Object
134 135 136 |
# File 'app/helpers/workbench/sessions_helper.rb', line 134 def is_project_member_by_id(user_id, project_id) ProjectMember.where(user_id:, project_id:).any? end |
#is_superuser? ⇒ Boolean
A superuser is an administrator or a person who is a project_administrator IN THE CURRENTLY SELECTED PROJECT
126 127 128 |
# File 'app/helpers/workbench/sessions_helper.rb', line 126 def is_superuser? sessions_signed_in? && ( is_administrator? || is_project_administrator? ) end |
#project_settings_link ⇒ Object
211 212 213 |
# File 'app/helpers/workbench/sessions_helper.rb', line 211 def project_settings_link (sessions_project_selected? && is_superuser?) ? link_to('Project', project_path(sessions_current_project)) : nil end |
#require_administrator_sign_in ⇒ Object
160 161 162 |
# File 'app/helpers/workbench/sessions_helper.rb', line 160 def require_administrator_sign_in redirect_to root_url, notice: 'Please sign in as an administrator.' unless is_administrator? end |
#require_project_administrator_sign_in ⇒ Object
164 165 166 |
# File 'app/helpers/workbench/sessions_helper.rb', line 164 def require_project_administrator_sign_in redirect_to root_url, notice: 'Please sign in as a project administrator.' unless is_project_administrator? end |
#require_project_selection ⇒ Object
146 147 148 |
# File 'app/helpers/workbench/sessions_helper.rb', line 146 def require_project_selection redirect_to root_url, notice: 'Please select a project.' unless sessions_current_project end |
#require_sign_in ⇒ Object
142 143 144 |
# File 'app/helpers/workbench/sessions_helper.rb', line 142 def require_sign_in redirect_to root_url, notice: 'Please sign in.' unless sessions_signed_in? end |
#require_sign_in_and_project_selection ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'app/helpers/workbench/sessions_helper.rb', line 150 def require_sign_in_and_project_selection # TODO: account for permitted token based projects unless (sessions_signed_in? or @api_request) && sessions_project_selected? respond_to do |format| format.html { redirect_to root_url, notice: 'Whoa there, sign in and select a project first.' } format.json { render json: { error: 'Whoa there, sign in and select a project first.' }, status: :unauthorized } # TODO: bad request, not unauthorized end end end |
#require_superuser_sign_in ⇒ Object
168 169 170 |
# File 'app/helpers/workbench/sessions_helper.rb', line 168 def require_superuser_sign_in redirect_to root_url, notice: 'Please sign in as a project administrator or administrator.' unless is_superuser? end |
#session_header_links ⇒ Object
TODO: make this a non-controller method
178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/helpers/workbench/sessions_helper.rb', line 178 def session_header_links [ project_settings_link, administration_link, link_to('Account', sessions_current_user, data: { current_user_id: sessions_current_user.id.to_s, current_user_is_administrator: sessions_current_user.is_administrator, }), link_to('Sign out', signout_path, method: :delete, id: 'sign_out') ] end |
#sessions_clear_selected_project ⇒ Object
102 103 104 105 106 107 108 |
# File 'app/helpers/workbench/sessions_helper.rb', line 102 def sessions_clear_selected_project if @api_request @sessions_current_project = nil else session[:project_id] = nil end end |
#sessions_current_project ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/helpers/workbench/sessions_helper.rb', line 88 def sessions_current_project return nil unless sessions_current_project_id if @sessions_current_project.nil? || @sessions_current_project.id != sessions_current_project_id @sessions_current_project = Project.find(sessions_current_project_id) end @sessions_current_project end |
#sessions_current_project_id ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/helpers/workbench/sessions_helper.rb', line 80 def sessions_current_project_id if @api_request return @sessions_current_project.id if @sessions_current_project else session[:project_id] end end |
#sessions_current_project_id=(project_id) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'app/helpers/workbench/sessions_helper.rb', line 71 def sessions_current_project_id=(project_id) if @api_request @sessions_current_project = Project.find(project_id) else session[:project_id] = project_id end project_id end |
#sessions_current_user ⇒ Object Also known as: current_user
13 14 15 |
# File 'app/helpers/workbench/sessions_helper.rb', line 13 def sessions_current_user @sessions_current_user ||= User.find_by(remember_token: User.encrypt([:remember_token])) end |
#sessions_current_user=(user) ⇒ Object
9 10 11 |
# File 'app/helpers/workbench/sessions_helper.rb', line 9 def sessions_current_user=(user) @sessions_current_user = user end |
#sessions_current_user_id ⇒ Object
21 22 23 |
# File 'app/helpers/workbench/sessions_helper.rb', line 21 def sessions_current_user_id sessions_current_user ? sessions_current_user.id : nil end |
#sessions_project_selected? ⇒ Boolean
67 68 69 |
# File 'app/helpers/workbench/sessions_helper.rb', line 67 def sessions_project_selected? !sessions_current_project_id.nil? end |
#sessions_select_project(project) ⇒ Object
97 98 99 100 |
# File 'app/helpers/workbench/sessions_helper.rb', line 97 def sessions_select_project(project) self.sessions_current_project_id = project.id sessions_current_project end |
#sessions_sign_in(user, request) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/helpers/workbench/sessions_helper.rb', line 25 def sessions_sign_in(user, request) remember_token = User.secure_random_token .permanent[:remember_token] = remember_token c = { remember_token: User.encrypt(remember_token), sign_in_count: (user.sign_in_count + 1), last_sign_in_at: user.current_sign_in_at, current_sign_in_at: Time.now, last_sign_in_ip: user.current_sign_in_ip, current_sign_in_ip: request.ip, } # TODO set to zero on User create to eliminate need for this c[:time_active] = 0 if user.time_active.blank? user.update_columns( c ) self.sessions_current_user = user end |
#sessions_sign_out ⇒ Object
46 47 48 49 50 |
# File 'app/helpers/workbench/sessions_helper.rb', line 46 def sessions_sign_out self.sessions_current_user = nil sessions_clear_selected_project .delete(:remember_token) end |
#sessions_signed_in? ⇒ Boolean
User methods
5 6 7 |
# File 'app/helpers/workbench/sessions_helper.rb', line 5 def sessions_signed_in? !sessions_current_user.nil? end |
#set_project_from_params ⇒ Object
Project methods
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/workbench/sessions_helper.rb', line 54 def set_project_from_params # Ensure project_token and project_id are the same if provided. # TODO: Community data considerations if sessions_current_project_id respond_to do |format| format.html { redirect_to root_url, notice: 'Project token and project are not the same.' } format.json { render(json: {success: false}, status: :bad_request) && return } # was unauthorized end else self.sessions_current_project_id = params[:project_id] end end |