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)

Instance Method Details

- (Boolean) administers_projects?

Returns:

  • (Boolean)


119
120
121
# File 'app/helpers/workbench/sessions_helper.rb', line 119

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


206
207
208
# File 'app/helpers/workbench/sessions_helper.rb', line 206

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

- (Object) authorize_project_selection(user, project)



132
133
134
# File 'app/helpers/workbench/sessions_helper.rb', line 132

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)


167
168
169
# File 'app/helpers/workbench/sessions_helper.rb', line 167

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)


184
185
186
187
188
189
190
# File 'app/helpers/workbench/sessions_helper.rb', line 184

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)


198
199
200
# File 'app/helpers/workbench/sessions_helper.rb', line 198

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)


192
193
194
# File 'app/helpers/workbench/sessions_helper.rb', line 192

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)


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

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)


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

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)


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

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)


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

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


202
203
204
# File 'app/helpers/workbench/sessions_helper.rb', line 202

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

- (Object) require_administrator_sign_in



154
155
156
# File 'app/helpers/workbench/sessions_helper.rb', line 154

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

- (Object) require_project_administrator_sign_in



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

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

- (Object) require_project_selection



140
141
142
# File 'app/helpers/workbench/sessions_helper.rb', line 140

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

- (Object) require_sign_in



136
137
138
# File 'app/helpers/workbench/sessions_helper.rb', line 136

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

- (Object) require_sign_in_and_project_selection



144
145
146
147
148
149
150
151
152
# File 'app/helpers/workbench/sessions_helper.rb', line 144

def 
  # 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: {success: false}, status: :unauthorized) && return } # TODO: bad request, not unauthorized
    end
  end
end

- (Object) require_superuser_sign_in



162
163
164
# File 'app/helpers/workbench/sessions_helper.rb', line 162

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



172
173
174
175
176
177
178
179
# File 'app/helpers/workbench/sessions_helper.rb', line 172

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



100
101
102
103
104
105
106
# File 'app/helpers/workbench/sessions_helper.rb', line 100

def sessions_clear_selected_project
  if @api_request
    @sessions_current_project = nil
  else
    session[:project_id] = nil
  end
end

- (Object) sessions_current_project



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

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



78
79
80
81
82
83
84
# File 'app/helpers/workbench/sessions_helper.rb', line 78

def sessions_current_project_id
  if @api_request
    return @sessions_current_project.id if @sessions_current_project
  else
    session[:project_id]
  end
end

- (Object) sessions_current_project_id=(project_id)



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

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

- (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



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

- (Boolean) sessions_project_selected?

Returns:

  • (Boolean)


65
66
67
# File 'app/helpers/workbench/sessions_helper.rb', line 65

def sessions_project_selected?
  !sessions_current_project_id.nil?
end

- (Object) sessions_select_project(project)



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

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

- (Object) sessions_sign_in(user, request)



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 (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



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
  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

- (Object) set_project_from_params

Project methods



54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/workbench/sessions_helper.rb', line 54

def set_project_from_params
  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: :unauthorized) && return } # TODO: bad request, not unauthorized
    end
  else
    self.sessions_current_project_id = params[:project_id]
  end
end