openstax_connect 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ module OpenStax
5
5
  module Connect
6
6
 
7
7
  # Inherit from the applications ApplicationController to share some methods
8
- class ApplicationController < ::ApplicationController
8
+ class ApplicationController < ActionController::Base
9
9
  include Lev::HandleWith
10
10
  end
11
11
 
@@ -15,30 +15,34 @@ module OpenStax
15
15
  handle_with(SessionsOmniauthAuthenticated,
16
16
  complete: lambda {
17
17
  sign_in(@handler_result.outputs[:connect_user_to_sign_in])
18
- redirect_to return_path(true)
18
+ redirect_to return_url(true)
19
19
  })
20
20
  end
21
21
 
22
22
  def destroy
23
23
  sign_out!
24
- redirect_to return_path, notice: "Signed out!"
24
+ # Need to sign out on the services site so can't log back in automagically
25
+ redirect_to OpenStax::Utilities.generate_url(
26
+ OpenStax::Connect.configuration.openstax_services_url + "/logout",
27
+ return_to: return_url
28
+ )
25
29
  end
26
30
 
27
31
  def failure
28
- redirect_to return_path, alert: "Authentication failed, please try again."
32
+ redirect_to return_url, alert: "Authentication failed, please try again."
29
33
  end
30
34
 
31
35
  def become
32
36
  raise SecurityTransgression unless !Rails.env.production? || current_user.is_administrator?
33
37
  sign_in(User.find(params[:user_id]))
34
- redirect_to return_path(true)
38
+ redirect_to return_url(true)
35
39
  end
36
40
 
37
41
  protected
38
42
 
39
- def return_path(include_referrer=false)
43
+ def return_url(include_referrer=false)
40
44
  referrer = include_referrer ? request.referrer : nil
41
- params[:return_to] || session.delete(:return_to) || referrer || main_app.root_path
45
+ params[:return_to] || session.delete(:return_to) || referrer || main_app.root_url
42
46
  end
43
47
 
44
48
  end
@@ -2,6 +2,8 @@ module OpenStax::Connect::Dev
2
2
  class UsersCreate
3
3
  lev_handler
4
4
 
5
+ # this code might be OBE
6
+
5
7
  protected
6
8
 
7
9
  def authorized?
@@ -13,7 +15,7 @@ module OpenStax::Connect::Dev
13
15
  user.first_name = params[:register][:first_name]
14
16
  user.last_name = params[:register][:last_name]
15
17
  user.username = params[:register][:username]
16
- user.is_administrator = params[:register][:is_administrator]
18
+ # user.is_administrator = params[:register][:is_administrator]
17
19
  user.openstax_uid = available_openstax_uid
18
20
  end
19
21
 
@@ -0,0 +1,33 @@
1
+ module OpenStax::Connect::Dev
2
+ class CreateUser
3
+ lev_routine
4
+
5
+ protected
6
+
7
+ def exec(inputs={})
8
+
9
+ username = inputs[:username]
10
+
11
+ if username.nil? || inputs[:ensure_no_errors]
12
+ loop do
13
+ break if !username.nil? && OpenStax::Connect::User.where(username: username).none?
14
+ username = "#{inputs[:username] || 'user'}#{rand(1000000)}"
15
+ end
16
+ end
17
+
18
+ outputs[:user] = OpenStax::Connect::User.create do |user|
19
+ user.first_name = inputs[:first_name]
20
+ user.last_name = inputs[:last_name]
21
+ user.username = username
22
+ user.openstax_uid = available_negative_openstax_uid
23
+ end
24
+
25
+ transfer_errors_from(outputs[:user], {type: :verbatim})
26
+ end
27
+
28
+ def available_negative_openstax_uid
29
+ (OpenStax::Connect::User.order("openstax_uid DESC").last.try(:openstax_uid) || 0) - 1
30
+ end
31
+
32
+ end
33
+ end
@@ -12,8 +12,8 @@ module OpenStax::Connect
12
12
 
13
13
  # Returns the current app user
14
14
  def current_user
15
- refresh_current_users if @current_app_user.nil?
16
- @current_app_user
15
+ load_current_users
16
+ @app_current_user
17
17
  end
18
18
 
19
19
  # Signs in the given user; the argument can be either a connect user or
@@ -36,51 +36,46 @@ module OpenStax::Connect
36
36
 
37
37
  protected
38
38
 
39
- # Refreshes the current connect user (if needed) and returns it.
39
+ # Returns the current connect user
40
40
  def connect_current_user
41
- refresh_current_users if @connect_current_user.nil?
41
+ load_current_users
42
42
  @connect_current_user
43
43
  end
44
44
 
45
- def refresh_current_users
45
+ # If they are nil (unset), sets the current users based on the session state
46
+ def load_current_users
47
+ return if !@connect_current_user.nil?
48
+
46
49
  if @request.ssl? && @cookies.signed[:secure_user_id] != "secure#{@session[:user_id]}"
47
50
  sign_out! # hijacked
48
51
  else
49
- new_connect_current_user = @connect_current_user || User.anonymous
50
- new_connect_current_user = User.where(id: @session[:user_id]).first \
51
- if new_connect_current_user.is_anonymous? && @session[:user_id]
52
-
53
- # changes both current and app user
54
- self.connect_current_user = new_connect_current_user
52
+ @connect_current_user = @session[:user_id] ?
53
+ User.where(id: @session[:user_id]).first :
54
+ User.anonymous
55
+ @app_current_user = user_provider.connect_user_to_app_user(@connect_current_user)
55
56
  end
56
57
  end
57
58
 
58
59
  # Sets (signs in) the provided app user.
59
60
  def current_user=(user)
60
61
  self.connect_current_user = user_provider.app_user_to_connect_user(user)
61
- @current_app_user
62
+ @app_current_user
62
63
  end
63
64
 
64
- # Sets the current connect user, updating the session and cookie state, also
65
- # updates the cache of the current app user.
65
+ # Sets the current connect user, updates the app user, and also updates the
66
+ # session and cookie state.
66
67
  def connect_current_user=(user)
67
- user ||= User.anonymous
68
- @connect_current_user ||= User.anonymous
69
-
70
- if user != @connect_current_user
71
- @connect_current_user = user
72
- @current_app_user = nil # changed connect user so invalidate the app user
73
-
74
- if @connect_current_user.is_anonymous?
75
- @session[:user_id] = nil
76
- @cookies.delete(:secure_user_id)
77
- else
78
- @session[:user_id] = @connect_current_user.id
79
- @cookies.signed[:secure_user_id] = {secure: true, value: "secure#{@connect_current_user.id}"}
80
- end
68
+ @connect_current_user = user || User.anonymous
69
+ @app_current_user = user_provider.connect_user_to_app_user(@connect_current_user)
70
+
71
+ if @connect_current_user.is_anonymous?
72
+ @session[:user_id] = nil
73
+ @cookies.delete(:secure_user_id)
74
+ else
75
+ @session[:user_id] = @connect_current_user.id
76
+ @cookies.signed[:secure_user_id] = {secure: true, value: "secure#{@connect_current_user.id}"}
81
77
  end
82
78
 
83
- @current_app_user ||= user_provider.connect_user_to_app_user(@connect_current_user)
84
79
  @connect_current_user
85
80
  end
86
81
 
@@ -1,5 +1,5 @@
1
1
  module OpenStax
2
2
  module Connect
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstax_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-09 00:00:00.000000000 Z
12
+ date: 2013-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -146,11 +146,11 @@ files:
146
146
  - app/helpers/openstax/connect/application_helper.rb
147
147
  - app/helpers/openstax/connect/sessions_helper.rb
148
148
  - app/models/openstax/connect/user.rb
149
+ - app/routines/openstax/connect/dev/create_user.rb
149
150
  - app/routines/openstax/connect/search_users.rb
150
151
  - app/views/layouts/openstax/connect/application.html.erb
151
152
  - app/views/openstax/connect/dev/users/index.html.erb
152
153
  - app/views/openstax/connect/dev/users/search.js.erb
153
- - app/views/openstax/connect/sessions/create.html.erb
154
154
  - app/views/openstax/connect/shared/_attention.html.erb
155
155
  - app/views/openstax/connect/users/_action_create_form.html.erb
156
156
  - app/views/openstax/connect/users/_action_dialog.html.erb
@@ -189,7 +189,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
189
189
  version: '0'
190
190
  segments:
191
191
  - 0
192
- hash: 3664854018823148928
192
+ hash: 1060826092135377802
193
193
  required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  none: false
195
195
  requirements:
@@ -198,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
198
  version: '0'
199
199
  segments:
200
200
  - 0
201
- hash: 3664854018823148928
201
+ hash: 1060826092135377802
202
202
  requirements: []
203
203
  rubyforge_project:
204
204
  rubygems_version: 1.8.25
@@ -1,10 +0,0 @@
1
- <h1>Sessions#create</h1>
2
- <p>Find me in app/views/openstax/connect/sessions/create.html.erb</p>
3
-
4
- <hr/>
5
-
6
- <%= request.env['omniauth.auth'] %>
7
-
8
- <hr/>
9
-
10
- <%= session.inspect %>