Module: Workbench::SessionsHelper

Included in:
ApplicationController
Defined in:
app/helpers/workbench/sessions_helper.rb

Overview

These are controller methods.

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) administers_projects?

Returns:

  • (Boolean)


92
93
94
# File 'app/helpers/workbench/sessions_helper.rb', line 92

def administers_projects?
  sessions_signed_in? && (is_administrator? || sessions_current_user.administers_projects? )
end


173
174
175
# File 'app/helpers/workbench/sessions_helper.rb', line 173

def administration_link
  sessions_current_user.is_administrator? ? link_to('Administration', administration_path) : nil
end

- (Object) authorize_project_selection(user, project)



105
106
107
# File 'app/helpers/workbench/sessions_helper.rb', line 105

def authorize_project_selection(user, project)
  project.project_members.where(user: user, project: project)
end

- (Boolean) can_administer_projects?

User is some project_administrator or administrator

Returns:

  • (Boolean)


134
135
136
# File 'app/helpers/workbench/sessions_helper.rb', line 134

def can_administer_projects?
  redirect_to root_url, notice: 'Please sign in as a project administrator or administrator.' unless administers_projects?
end

TODO: NOT here

Parameters:

  • (String)
  • (String)


151
152
153
154
155
156
157
# File 'app/helpers/workbench/sessions_helper.rb', line 151

def favorite_page_link(kind, name)
  if favorites?(kind, name)
    link_to('Unfavorite page', unfavorite_page_path(kind: kind, name: 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: kind, name: name), method: :post, remote: true, id: "favorite_link_#{kind}-#{name}", class: :favourite_link, title: 'Add to favorite.')
  end
end

- (Boolean) favorites?(kind, name)

Parameters:

  • (String)
  • (String)

Returns:

  • (Boolean)


165
166
167
# File 'app/helpers/workbench/sessions_helper.rb', line 165

def favorites?(kind, name)
  has_hub_favorites? && sessions_current_user.hub_favorites[sessions_current_project_id.to_s][kind].include?(name)
end

- (Boolean) has_hub_favorites?

Returns:

  • (Boolean)


159
160
161
# File 'app/helpers/workbench/sessions_helper.rb', line 159

def has_hub_favorites?
  sessions_current_user.hub_favorites[sessions_current_project_id.to_s] ? true : false
end

- (Boolean) is_administrator?

Authorization methods

Returns:

  • (Boolean)


82
83
84
# File 'app/helpers/workbench/sessions_helper.rb', line 82

def is_administrator?
  sessions_signed_in? && sessions_current_user.is_administrator?
end

- (Boolean) is_project_administrator?

Can be optimized to just look at ProjectMembers likely

Returns:

  • (Boolean)


87
88
89
90
# File 'app/helpers/workbench/sessions_helper.rb', line 87

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

- (Boolean) is_project_member?(user, project)

Returns:

  • (Boolean)


101
102
103
# File 'app/helpers/workbench/sessions_helper.rb', line 101

def is_project_member?(user, project)
  project.project_members.include?(user)
end

- (Boolean) is_superuser?

A superuser is an administrator or a person who is a project_administrator IN THE CURRENTLY SELECTED PROJECT

Returns:

  • (Boolean)


97
98
99
# File 'app/helpers/workbench/sessions_helper.rb', line 97

def is_superuser?
  sessions_signed_in? && ( is_administrator? || is_project_administrator? )
end


169
170
171
# File 'app/helpers/workbench/sessions_helper.rb', line 169

def project_settings_link
  (sessions_project_selected? && is_superuser?) ? link_to('Project', project_path(sessions_current_project)) : nil
end

- (Object) require_administrator_sign_in



121
122
123
# File 'app/helpers/workbench/sessions_helper.rb', line 121

def 
  redirect_to root_url, notice: 'Please sign in as an administrator.' unless is_administrator?
end

- (Object) require_project_administrator_sign_in



125
126
127
# File 'app/helpers/workbench/sessions_helper.rb', line 125

def 
  redirect_to root_url, notice: 'Please sign in as a project administrator.' unless is_project_administrator?
end

- (Object) require_project_selection



113
114
115
# File 'app/helpers/workbench/sessions_helper.rb', line 113

def require_project_selection
  redirect_to root_url, notice: 'Please select a project.' unless sessions_current_project
end

- (Object) require_sign_in



109
110
111
# File 'app/helpers/workbench/sessions_helper.rb', line 109

def 
  redirect_to root_url, notice: 'Please sign in.' unless sessions_signed_in?
end

- (Object) require_sign_in_and_project_selection



117
118
119
# File 'app/helpers/workbench/sessions_helper.rb', line 117

def 
  redirect_to root_url, notice: 'Whoa there, sign in and select a project first.' unless sessions_signed_in? && sessions_project_selected?
end

- (Object) require_superuser_sign_in



129
130
131
# File 'app/helpers/workbench/sessions_helper.rb', line 129

def 
  redirect_to root_url, notice: 'Please sign in as a project administrator or administrator.' unless is_superuser?
end

TODO: make this a non-controller method



139
140
141
142
143
144
145
146
# File 'app/helpers/workbench/sessions_helper.rb', line 139

def session_header_links
  [
    project_settings_link,
    administration_link,
    link_to('Account', sessions_current_user),
    link_to('Sign out', signout_path, method: :delete, id: 'sign_out')
  ]
end

- (Object) sessions_clear_selected_project



77
78
79
# File 'app/helpers/workbench/sessions_helper.rb', line 77

def sessions_clear_selected_project
  session[:project_id] = nil
end

- (Object) sessions_current_project



64
65
66
67
68
69
70
# File 'app/helpers/workbench/sessions_helper.rb', line 64

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

- (Object) sessions_current_project_id



60
61
62
# File 'app/helpers/workbench/sessions_helper.rb', line 60

def sessions_current_project_id
  session[:project_id]
end

- (Object) sessions_current_project_id=(project_id)



56
57
58
# File 'app/helpers/workbench/sessions_helper.rb', line 56

def sessions_current_project_id=(project_id)
  session[:project_id] = project_id
end

- (Object) sessions_current_user 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(cookies[:remember_token]))
end

- (Object) sessions_current_user=(user)



9
10
11
# File 'app/helpers/workbench/sessions_helper.rb', line 9

def sessions_current_user=(user)
  @sessions_current_user = user
end

- (Object) sessions_current_user_id



20
21
22
# File 'app/helpers/workbench/sessions_helper.rb', line 20

def sessions_current_user_id
  sessions_current_user ? sessions_current_user.id : nil
end

- (Boolean) sessions_project_selected?

Project methods

Returns:

  • (Boolean)


52
53
54
# File 'app/helpers/workbench/sessions_helper.rb', line 52

def sessions_project_selected?
  !sessions_current_project_id.nil?
end

- (Object) sessions_select_project(project)



72
73
74
75
# File 'app/helpers/workbench/sessions_helper.rb', line 72

def sessions_select_project(project)
  self.sessions_current_project_id = project.id
 sessions_current_project
end

- (Object) sessions_sign_in(user, request)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/workbench/sessions_helper.rb', line 24

def (user, request)
  remember_token = User.secure_random_token
  cookies.permanent[:remember_token] = remember_token

  c = {
    remember_token: User.encrypt(remember_token),
    sign_in_count: (user. + 1),
    last_sign_in_at: user.,
    current_sign_in_at: Time.now,
    last_sign_in_ip: user.,
    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

- (Object) sessions_sign_out



45
46
47
48
49
# File 'app/helpers/workbench/sessions_helper.rb', line 45

def sessions_sign_out
  self.sessions_current_user = nil
  sessions_clear_selected_project
  cookies.delete(:remember_token)
end

- (Boolean) sessions_signed_in?

User methods

Returns:

  • (Boolean)


5
6
7
# File 'app/helpers/workbench/sessions_helper.rb', line 5

def sessions_signed_in?
  !sessions_current_user.nil?
end