openstax_accounts 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +89 -58
  3. data/app/controllers/openstax/accounts/application_controller.rb +3 -12
  4. data/app/controllers/openstax/accounts/dev/accounts_controller.rb +19 -0
  5. data/app/controllers/openstax/accounts/dev/base_controller.rb +2 -8
  6. data/app/controllers/openstax/accounts/sessions_controller.rb +31 -19
  7. data/app/handlers/openstax/accounts/dev/{users_index.rb → accounts_index.rb} +4 -4
  8. data/app/handlers/openstax/accounts/sessions_callback.rb +43 -0
  9. data/app/models/openstax/accounts/{user.rb → account.rb} +7 -22
  10. data/app/models/openstax/accounts/anonymous_account.rb +24 -0
  11. data/app/models/openstax/accounts/application_account.rb +7 -0
  12. data/app/representers/openstax/accounts/api/v1/{user_representer.rb → account_representer.rb} +1 -1
  13. data/app/representers/openstax/accounts/api/v1/{user_search_representer.rb → account_search_representer.rb} +7 -6
  14. data/app/representers/openstax/accounts/api/v1/{application_user_representer.rb → application_account_representer.rb} +5 -4
  15. data/app/representers/openstax/accounts/api/v1/application_account_search_representer.rb +20 -0
  16. data/app/representers/openstax/accounts/api/v1/{application_users_representer.rb → application_accounts_representer.rb} +3 -3
  17. data/app/routines/openstax/accounts/dev/{search_users.rb → search_accounts.rb} +5 -5
  18. data/app/routines/openstax/accounts/{search_users.rb → search_accounts.rb} +55 -46
  19. data/app/routines/openstax/accounts/sync_accounts.rb +47 -0
  20. data/app/views/openstax/accounts/dev/{users → accounts}/_search_results.html.erb +13 -13
  21. data/app/views/openstax/accounts/dev/{users/login.html.erb → accounts/index.html.erb} +3 -3
  22. data/app/views/openstax/accounts/dev/{users → accounts}/index.js.erb +1 -1
  23. data/app/views/openstax/accounts/shared/{users → accounts}/_index.html.erb +4 -1
  24. data/config/initializers/action_interceptor.rb +14 -0
  25. data/config/routes.rb +18 -15
  26. data/db/migrate/0_create_openstax_accounts_accounts.rb +22 -0
  27. data/lib/generators/openstax/accounts/schedule/USAGE +1 -1
  28. data/lib/generators/openstax/accounts/schedule/templates/schedule.rb +1 -1
  29. data/lib/openstax/accounts/current_user_manager.rb +74 -65
  30. data/lib/openstax/accounts/default_account_user_mapper.rb +15 -0
  31. data/lib/openstax/accounts/engine.rb +2 -0
  32. data/lib/openstax/accounts/extend_builtins.rb +37 -0
  33. data/lib/openstax/accounts/version.rb +1 -1
  34. data/lib/openstax_accounts.rb +46 -25
  35. data/spec/controllers/openstax/accounts/dev/accounts_controller_spec.rb +21 -0
  36. data/spec/controllers/openstax/accounts/sessions_controller_spec.rb +12 -9
  37. data/spec/dummy/app/controllers/api/application_users_controller.rb +0 -4
  38. data/spec/dummy/app/controllers/api/users_controller.rb +7 -0
  39. data/spec/dummy/app/models/anonymous_user.rb +48 -0
  40. data/spec/dummy/app/models/user.rb +26 -0
  41. data/spec/dummy/config/initializers/openstax_accounts.rb +3 -2
  42. data/spec/dummy/config/routes.rb +5 -3
  43. data/spec/dummy/db/migrate/1_create_users.rb +11 -0
  44. data/spec/dummy/db/schema.rb +18 -6
  45. data/spec/factories/openstax_accounts_account.rb +6 -0
  46. data/spec/lib/openstax/accounts/current_user_manager_spec.rb +151 -0
  47. data/spec/lib/openstax_accounts_spec.rb +16 -9
  48. data/spec/models/openstax/accounts/account_spec.rb +9 -0
  49. data/spec/models/openstax/accounts/anonymous_account_spec.rb +9 -0
  50. data/spec/routines/openstax/accounts/{search_users_spec.rb → search_accounts_spec.rb} +38 -38
  51. metadata +87 -50
  52. data/app/controllers/openstax/accounts/dev/users_controller.rb +0 -22
  53. data/app/handlers/openstax/accounts/sessions_omniauth_authenticated.rb +0 -48
  54. data/app/models/openstax/accounts/application_user.rb +0 -7
  55. data/app/representers/openstax/accounts/api/v1/application_user_search_representer.rb +0 -19
  56. data/app/routines/openstax/accounts/dev/create_user.rb +0 -37
  57. data/app/routines/openstax/accounts/sync_users.rb +0 -44
  58. data/config/initializers/extend_builtins.rb +0 -42
  59. data/db/migrate/0_create_openstax_accounts_users.rb +0 -18
  60. data/lib/openstax/accounts/action_list.rb +0 -42
  61. data/lib/openstax/accounts/route_helper.rb +0 -34
  62. data/lib/openstax/accounts/user_provider.rb +0 -15
  63. data/spec/controllers/openstax/accounts/dev/users_controller_spec.rb +0 -21
  64. data/spec/factories/openstax_accounts_user.rb +0 -6
  65. data/spec/models/openstax/accounts/user_spec.rb +0 -13
  66. data/spec/routines/openstax/accounts/dev/create_user_spec.rb +0 -26
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Mjc2YjE2MDk2NTlkODU2NWVkNjJhYjAzZjY2MzRiZmMxOTRmZDNkZg==
4
+ ZmE5M2NiMTk1MWUzMTg1OTY1MDA5MzVjMGExNzEwMmVlMDY2NTFiMA==
5
5
  data.tar.gz: !binary |-
6
- NzZmMzllNDRiODQ1MWVhMmRjMWNmYWYxYzc4ODUzZjNiMjkyZWFlNA==
6
+ MmMwMGI2MTRkYTkzZDg2NTlhZmE2Njc5NjZjOTgyMTUyNDJkZDU5MA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzgxYzZkMjI5NDc5NTk3ZjQyOGFjMDVjYThiNDg4NDY2MTM1MWRlZTI4YTQ2
10
- NDE2MzVkNDc4YzI1ODNkMDQ3ZWU0YjFkNmQxZmY5YWU2YTQzM2VmZjI4Zjk0
11
- MWI1MzkyMzQ0NzA1MmQxMGZkYjFiZmRlOGI4NDczYzc5ZjVlMjc=
9
+ NmZhOWE1OTA5Y2U5M2ViZTJlNzQzMjNjYmUwM2FkMmFlNTczNDU5ZDVmZmEz
10
+ NjFmNmI4NGU4OGRiNDE2OTI2ZDRhYTdiNjFlMjc1NTA3ZWFiM2E4NWM4OTY3
11
+ YTZhZTVlYjVjODhkY2YzMmYxZGRhZWQxNmM1YmUyNzE0MmJhOGE=
12
12
  data.tar.gz: !binary |-
13
- ODRkZDMzMDEwNGQwNTRhYjU5NzkwODU5YzljZGE4YzhiZWUwNDkzMzkwMWMx
14
- ZmEwMmNlYTUzZDVjY2VhYzg5MWJiNzJkZjcwYjQ3MjVmZjE0ZTAwOGJjOWRl
15
- MTE5ODIxN2NjYTY1MzFiZjI5NjNjMjE4ZTIwNTBlNzI5N2RlNDM=
13
+ NmJiODkwZGVjZTRlNzZmNGMwNDJmNWJkMTE1ZTE2MjQzNDc2YWM5YjRmYzlk
14
+ MjVjODkyYTNmNDZmMjQ4NTQyMDliY2YxYmY4MWNhMmY4NzA1OTcwMzNlNmFm
15
+ YTQwOTQxOWE5Y2NlYzJiMmQ2ZDdjYjBkYTZkOTg1ZTIxZTc2YTU=
data/README.md CHANGED
@@ -6,82 +6,115 @@ accounts-rails
6
6
 
7
7
  A rails engine for interfacing with OpenStax's accounts server.
8
8
 
9
+ Installation
10
+ ------------
11
+
12
+ Make sure to install the engine's migrations:
13
+
14
+ ```sh
15
+ $ rake openstax_accounts:install:migrations
16
+ ```
17
+
9
18
  Usage
10
19
  -----
11
20
 
12
- Add the engine to your Gemfile and then run `bundle install`.
21
+ Add the engine to your Gemfile and then run `bundle install`.
13
22
 
14
23
  Mount the engine your application's `routes.rb` file:
15
24
 
16
- MyApplication::Application.routes.draw do
17
- ...
18
- mount OpenStax::Accounts::Engine, at: "/accounts"
19
- ...
20
- end
25
+ ```rb
26
+ MyApplication::Application.routes.draw do
27
+ # ...
28
+ mount OpenStax::Accounts::Engine, at: "/accounts"
29
+ # ...
30
+ end
31
+ ```
21
32
 
22
- You can use whatever path you want instead of `/accounts`, just make sure to make the appropriate changes below.
33
+ You can use whatever path you want instead of `/accounts`,
34
+ just make sure to make the appropriate changes below.
23
35
 
24
- Create an `openstax_accounts.rb` initializer in `config/initializers`, with the following contents:
36
+ Create an `openstax_accounts.rb` initializer in `config/initializers`,
37
+ with at least the following contents:
25
38
 
26
- OpenStax::Accounts.configure do |config|
27
- config.openstax_application_id = 'value_from_openstax_accounts_here'
28
- config.openstax_application_secret = 'value_from_openstax_accounts_here'
29
- end
39
+ ```rb
40
+ OpenStax::Accounts.configure do |config|
41
+ config.openstax_application_id = 'value_from_openstax_accounts_here'
42
+ config.openstax_application_secret = 'value_from_openstax_accounts_here'
43
+ end
44
+ ```
45
+
46
+ If you're running the OpenStax Accounts server in a dev instance on your
47
+ machine, you can specify that instance's local URL with:
30
48
 
31
- If you're running OpenStax Accounts in a dev instance on your machine, you can specify that instance's local URL with:
49
+ ```rb
50
+ config.openstax_accounts_url = 'http://localhost:2999/'
51
+ ```
52
+
53
+ To have users login, direct them to the login path using the
54
+ `openstax_accounts.login_path` route helper with Action Interceptor, e.g:
55
+
56
+ ```erb
57
+ <%= link_to 'Sign in!', with_interceptor { openstax_accounts.login_path } %>
58
+ ```
32
59
 
33
- config.openstax_accounts_url = 'http://localhost:2999/'
60
+ The `with_interceptor` block is necessary if you don't want to
61
+ depend on the user's browser setting referers properly.
34
62
 
35
- To have users login, direct them to `/accounts/sessions/new`. This is also available through the `openstax_accounts.login` route helper, e.g. `<%= link_to 'Sign in!', openstax_accounts.login_path %>`.
63
+ You can also add the `authenticate_user!` interceptor to your controllers:
36
64
 
37
- There is also a logout path helper for `/accounts/sessions/destroy`, given by `logout_path`. By default this expects a `GET` request. If you'd prefer a `DELETE` request, add this configuration:
65
+ ```rb
66
+ interceptor :authenticate_user!
67
+ ```
38
68
 
39
- config.logout_via = :delete
69
+ There is also a logout path helper, given by `logout_path`.
70
+ By default this expects a `GET` request.
71
+ If you'd prefer a `DELETE` request, add this configuration:
40
72
 
41
- OpenStax Accounts provides you with an `OpenStax::Accounts::User` object. You can
42
- use this as your app's User object without modification, you can modify it to suit
43
- your app's needs (not recommended), or you can provide your own custom User object
44
- that references the OpenStax Accounts User object.
73
+ ```rb
74
+ config.logout_via = :delete
75
+ ```
45
76
 
46
- OpenStax Accounts also provides you methods for getting and setting the current
47
- signed in user (`current_user` and `current_user=` methods). If you choose to create
48
- your own custom User object that references the User object provided by Accounts,
49
- you can teach OpenStax Accounts how to translate between your app's custom User
50
- object and OpenStax Accounts's built-in User object.
77
+ OpenStax Accounts provides you with an `OpenStax::Accounts::Account` object.
78
+ You can modify it and use this as your app's User object (not recommended),
79
+ or you can provide your own custom user object that references one account.
51
80
 
52
- To do this, you need to set a `user_provider` in this configuration.
81
+ OpenStax Accounts also provides you methods for getting and setting the current
82
+ signed in user (`current_user` and `current_user=` methods). If you choose to
83
+ create your own custom user object, you can teach this gem how to translate
84
+ between a user object and an account object.
53
85
 
54
- config.user_provider = MyUserProvider
86
+ To do this, you need to set a `account_user_mapper` in this configuration.
55
87
 
56
- The user_provider is a class that provides two class methods:
88
+ config.account_user_mapper = MyAccountUserMapper
57
89
 
58
- def self.accounts_user_to_app_user(accounts_user)
59
- # Converts the given accounts user to an app user.
60
- # If you want to cache the accounts_user in the app user,
90
+ The account_user_mapper is a class that provides two class methods:
91
+
92
+ def self.account_to_user(account)
93
+ # Converts the given account to a user.
94
+ # If you want to cache the account in the user,
61
95
  # this is the place to do it.
62
- # If no app user exists for this accounts user, one should
96
+ # If no user exists for this account, one should
63
97
  # be created.
64
98
  end
65
99
 
66
- def self.app_user_to_accounts_user(app_user)
67
- # Converts the given app user to an accounts user.
100
+ def self.user_to_account(user)
101
+ # Converts the given user to an account.
68
102
  end
69
103
 
70
- Accounts users are never nil. When a user is signed out, the current accounts user
71
- is an anonymous user (responding true to `is_anonymous?`). You can follow the same
72
- pattern in your app or you can use nil for the current user. Just remember to check
73
- the anonymous status of accounts users when doing your accounts <-> app translations.
74
-
75
- The default `user_provider` just uses OpenStax::Accounts::User as the app user.
104
+ Accounts are never nil. When a user is signed out, the current account is the
105
+ AnonymousAccount (responding true to `is_anonymous?`). You can follow the same
106
+ pattern in your app or you can use nil for the current user. Just remember to
107
+ check the anonymous status of accounts when doing your account <-> user
108
+ translations.
76
109
 
77
- Make sure to install the engine's migrations:
78
-
79
- rake openstax_accounts:install:migrations
110
+ The default `account_user_mapper` assumes the account object and
111
+ the user object are the same in your application.
80
112
 
81
113
  Syncing with Accounts
82
114
  ---------------------
83
115
 
84
- OpenStax::Accounts requires your app to periodically sync user information with the Accounts server. The easiest way to do this is to use the "whenever" gem.
116
+ OpenStax Accounts requires your app to periodically sync user information with
117
+ the Accounts server. The easiest way to do this is to use the "whenever" gem.
85
118
 
86
119
  To create or append to the schedule.rb file, run the following command:
87
120
 
@@ -89,7 +122,8 @@ To create or append to the schedule.rb file, run the following command:
89
122
  rails g openstax:accounts:schedule
90
123
  ```
91
124
 
92
- Then, after installing the "whenever" gem, run the `whenever` command for instructions to set up your crontab:
125
+ Then, after installing the "whenever" gem, run the `whenever` command for
126
+ instructions to set up your crontab:
93
127
 
94
128
  ```sh
95
129
  whenever
@@ -98,19 +132,16 @@ whenever
98
132
  Accounts API
99
133
  ------------
100
134
 
101
- OpenStax::Accounts provides convenience methods for accessing the OpenStax Accounts API.
102
-
103
- `OpenStax:: Accounts.create_application_user(accounts_user, version = nil)` takes
104
- an OpenStax::Accounts::User and, optionally, an API version argument, and creates
105
- an ApplicationUser for the configured application and the given user. Call this method
106
- when users finish the registration process in your app. This lets Accounts know that the given user is using your app, allowing Accounts to push user information to it whenever it changes.
135
+ OpenStax Accounts provides convenience methods for accessing
136
+ the Accounts server API.
107
137
 
108
- `OpenStax::Accounts.api_call(http_method, url, options = {})` provides a generic
109
- convenience method capable of making API calls to Accounts. `http_method` can be
110
- any valid HTTP method, and `url` is the desired API URL, without the 'api/' prefix.
111
- Options is a hash that can contain any option that OAuth2 requests accept, such
112
- as :headers, :params, :body, etc, plus the optional values :api_version (to specify
113
- an API version) and :access_token (to specify an OAuth access token).
138
+ `OpenStax::Accounts.api_call(http_method, url, options = {})` provides a
139
+ convenience method capable of making API calls to Accounts. `http_method` can
140
+ be any valid HTTP method, and `url` is the desired API URL, without the 'api/'
141
+ prefix. Options is a hash that can contain any option that
142
+ OAuth2 requests accept, such as :headers, :params, :body, etc,
143
+ plus the optional values :api_version (to specify an API version) and
144
+ :access_token (to specify an OAuth access token).
114
145
 
115
146
  Example Application
116
147
  -------------------
@@ -2,21 +2,12 @@ module OpenStax
2
2
  module Accounts
3
3
 
4
4
  class ApplicationController < ActionController::Base
5
- include Lev::HandleWith
6
5
 
7
- # Override current_user to always return an OpenStax::Accounts::User
8
- def current_user
9
- current_user_manager.accounts_current_user
10
- end
6
+ include Lev::HandleWith
11
7
 
12
- protected
8
+ acts_as_interceptor :override_url_options => false
13
9
 
14
- def return_url(include_referrer=false)
15
- referrer = include_referrer ? request.referrer : nil
16
- # Always clear the session
17
- session_return_to = session.delete(:return_to)
18
- params[:return_to] || session_return_to || referrer || main_app.root_url
19
- end
10
+ skip_interceptor :authenticate_user!
20
11
 
21
12
  end
22
13
 
@@ -0,0 +1,19 @@
1
+ module OpenStax
2
+ module Accounts
3
+ module Dev
4
+ class AccountsController < OpenStax::Accounts::Dev::BaseController
5
+
6
+ def index
7
+ handle_with(AccountsIndex)
8
+ end
9
+
10
+ def become
11
+ @account = Account.find(params[:id])
12
+ sign_in(@account)
13
+ redirect_back
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -3,15 +3,9 @@ module OpenStax
3
3
  module Dev
4
4
  class BaseController < OpenStax::Accounts::ApplicationController
5
5
 
6
- before_filter Proc.new{
6
+ before_filter do
7
7
  raise SecurityTransgression if Rails.env.production?
8
- }
9
-
10
- skip_before_filter :authenticate_user!
11
- skip_before_filter :require_registration!
12
-
13
- fine_print_skip_signatures :general_terms_of_use, :privacy_policy \
14
- if respond_to?(:fine_print_skip_signatures)
8
+ end
15
9
 
16
10
  end
17
11
  end
@@ -3,20 +3,23 @@ module OpenStax
3
3
  class SessionsController < OpenStax::Accounts::ApplicationController
4
4
 
5
5
  def new
6
- session[:return_to] ||= request.referrer
7
- redirect_to RouteHelper.get_path(:login)
6
+ if OpenStax::Accounts.configuration.enable_stubbing?
7
+ with_interceptor { redirect_to dev_accounts_path }
8
+ else
9
+ redirect_to openstax_login_path
10
+ end
8
11
  end
9
12
 
10
- def omniauth_authenticated
11
- handle_with(SessionsOmniauthAuthenticated,
12
- success: lambda {
13
- sign_in(@handler_result.outputs[:accounts_user_to_sign_in])
14
- # referrer is in Accounts, so don't include it
15
- redirect_to return_url
16
- },
17
- failure: lambda {
18
- failure
19
- })
13
+ def callback
14
+ handle_with(
15
+ SessionsCallback,
16
+ success: lambda {
17
+ sign_in(@handler_result.outputs[:account])
18
+ redirect_back
19
+ },
20
+ failure: lambda {
21
+ failure
22
+ })
20
23
  end
21
24
 
22
25
  def destroy
@@ -24,16 +27,25 @@ module OpenStax
24
27
 
25
28
  # If we're using the Accounts server, need to sign out of it so can't
26
29
  # log back in automagically
27
- redirect_to OpenStax::Accounts.configuration.enable_stubbing? ?
28
- return_url :
29
- OpenStax::Utilities.generate_url(
30
- OpenStax::Accounts.configuration.openstax_accounts_url + "/logout",
31
- return_to: return_url
32
- )
30
+ if OpenStax::Accounts.configuration.enable_stubbing?
31
+ redirect_back
32
+ else
33
+ without_interceptor do
34
+
35
+ redirect_to(
36
+ OpenStax::Utilities.generate_url(
37
+ OpenStax::Accounts.configuration.openstax_accounts_url,
38
+ "logout",
39
+ return_to: intercepted_url
40
+ )
41
+ )
42
+
43
+ end
44
+ end
33
45
  end
34
46
 
35
47
  def failure
36
- redirect_to return_url, alert: "Authentication failed, please try again."
48
+ redirect_back alert: "Authentication failed, please try again."
37
49
  end
38
50
 
39
51
  end
@@ -1,7 +1,7 @@
1
1
  module OpenStax
2
2
  module Accounts
3
3
  module Dev
4
- class UsersIndex
4
+ class AccountsIndex
5
5
 
6
6
  lev_handler transaction: :no_transaction
7
7
 
@@ -11,8 +11,8 @@ module OpenStax
11
11
  attribute :page, type: Integer
12
12
  end
13
13
 
14
- uses_routine OpenStax::Accounts::Dev::SearchUsers,
15
- as: :search_users,
14
+ uses_routine OpenStax::Accounts::Dev::SearchAccounts,
15
+ as: :search_accounts,
16
16
  translations: { outputs: {type: :verbatim} }
17
17
 
18
18
  protected
@@ -30,7 +30,7 @@ module OpenStax
30
30
  else
31
31
  query = search_params.terms || ''
32
32
  end
33
- run(:search_users, query, page: search_params.page || 0)
33
+ run(:search_accounts, query, page: search_params.page || 0)
34
34
  end
35
35
 
36
36
  end
@@ -0,0 +1,43 @@
1
+ module OpenStax
2
+ module Accounts
3
+
4
+ class SessionsCallback
5
+
6
+ lev_handler
7
+
8
+ protected
9
+
10
+ def setup
11
+ @auth_data = request.env['omniauth.auth']
12
+ end
13
+
14
+ def authorized?
15
+ @auth_data.provider == "openstax"
16
+ end
17
+
18
+ def handle
19
+ # Don't worry if the account is logged in or not beforehand.
20
+ # Just assume that they aren't.
21
+
22
+ existing_account = Account.where(openstax_uid: @auth_data.uid).first
23
+ return outputs[:account] = existing_account if !existing_account.nil?
24
+
25
+ new_account = Account.create do |account|
26
+ account.openstax_uid = @auth_data.uid
27
+ account.username = @auth_data.info.nickname
28
+ account.first_name = @auth_data.info.first_name
29
+ account.last_name = @auth_data.info.last_name
30
+ account.full_name = @auth_data.info.name
31
+ account.title = @auth_data.info.title
32
+ account.access_token = @auth_data.credentials.token
33
+ end
34
+
35
+ transfer_errors_from(new_account, {type: :verbatim})
36
+
37
+ outputs[:account] = new_account
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -1,14 +1,15 @@
1
1
  module OpenStax
2
2
  module Accounts
3
- class User < ActiveRecord::Base
3
+ class Account < ActiveRecord::Base
4
4
 
5
5
  USERNAME_DISCARDED_CHAR_REGEX = /[^A-Za-z\d_]/
6
6
  USERNAME_MAX_LENGTH = 50
7
7
 
8
- attr_accessor :updating_from_accounts
8
+ attr_accessor :syncing_with_accounts
9
9
 
10
10
  validates :username, uniqueness: true, presence: true
11
- validates :openstax_uid, presence: true
11
+ validates :openstax_uid, uniqueness: true, presence: true
12
+ validates :access_token, uniqueness: true, allow_nil: true
12
13
 
13
14
  attr_accessible :username, :first_name, :last_name, :full_name, :title
14
15
 
@@ -23,30 +24,14 @@ module OpenStax
23
24
  end
24
25
 
25
26
  def is_anonymous?
26
- is_anonymous == true
27
- end
28
-
29
- attr_accessor :is_anonymous
30
-
31
- def self.anonymous
32
- @@anonymous ||= AnonymousUser.new
27
+ false
33
28
  end
34
29
 
35
30
  def update_openstax_accounts
36
- return if updating_from_accounts || \
31
+ return if syncing_with_accounts || \
37
32
  OpenStax::Accounts.configuration.enable_stubbing?
38
- OpenStax::Accounts.user_update(self)
39
- end
40
33
 
41
- class AnonymousUser < User
42
- before_save { false }
43
- def initialize(attributes=nil)
44
- super
45
- self.is_anonymous = true
46
- self.first_name = 'Guest'
47
- self.last_name = 'User'
48
- self.openstax_uid = nil
49
- end
34
+ OpenStax::Accounts.update_account(self)
50
35
  end
51
36
 
52
37
  end
@@ -0,0 +1,24 @@
1
+ module OpenStax
2
+ module Accounts
3
+ class AnonymousAccount < Account
4
+
5
+ include Singleton
6
+
7
+ before_save { false }
8
+
9
+ def initialize(attributes=nil)
10
+ super
11
+ self.id = nil
12
+ self.openstax_uid = nil
13
+ self.username = 'anonymous'
14
+ self.first_name = 'Guest'
15
+ self.last_name = 'User'
16
+ end
17
+
18
+ def is_anonymous?
19
+ true
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module OpenStax
2
+ module Accounts
3
+ class ApplicationAccount
4
+ attr_accessor :id, :application_id, :account, :unread_updates, :default_contact_info_id
5
+ end
6
+ end
7
+ end
@@ -2,7 +2,7 @@ module OpenStax
2
2
  module Accounts
3
3
  module Api
4
4
  module V1
5
- class UserRepresenter < Roar::Decorator
5
+ class AccountRepresenter < Roar::Decorator
6
6
  include Roar::Representer::JSON
7
7
 
8
8
  property :openstax_uid,
@@ -2,10 +2,11 @@ module OpenStax
2
2
  module Accounts
3
3
  module Api
4
4
  module V1
5
- class UserSearchRepresenter < Roar::Decorator
5
+ class AccountSearchRepresenter < Roar::Decorator
6
6
  include Roar::Representer::JSON
7
7
 
8
- property :num_matching_users,
8
+ property :num_matching_accounts,
9
+ as: :num_matching_users,
9
10
  type: Integer
10
11
 
11
12
  property :page,
@@ -17,10 +18,10 @@ module OpenStax
17
18
  property :order_by,
18
19
  type: String
19
20
 
20
-
21
- collection :users,
22
- class: OpenStax::Accounts::User,
23
- decorator: UserRepresenter
21
+ collection :accounts,
22
+ as: :users,
23
+ class: Account,
24
+ decorator: AccountRepresenter
24
25
 
25
26
  end
26
27
  end
@@ -2,7 +2,7 @@ module OpenStax
2
2
  module Accounts
3
3
  module Api
4
4
  module V1
5
- class ApplicationUserRepresenter < Roar::Decorator
5
+ class ApplicationAccountRepresenter < Roar::Decorator
6
6
  include Roar::Representer::JSON
7
7
 
8
8
  property :id,
@@ -11,9 +11,10 @@ module OpenStax
11
11
  property :application_id,
12
12
  type: Integer
13
13
 
14
- property :user,
15
- class: OpenStax::Accounts::User,
16
- decorator: UserRepresenter
14
+ property :account,
15
+ as: :user,
16
+ class: OpenStax::Accounts::Account,
17
+ decorator: AccountRepresenter
17
18
 
18
19
  property :unread_updates,
19
20
  type: Integer
@@ -0,0 +1,20 @@
1
+ module OpenStax
2
+ module Accounts
3
+ module Api
4
+ module V1
5
+ class ApplicationAccountSearchRepresenter < AccountSearchRepresenter
6
+
7
+ collection :application_accounts,
8
+ as: :application_users,
9
+ class: OpenStax::Accounts::ApplicationAccount,
10
+ decorator: ApplicationAccountRepresenter,
11
+ schema_info: {
12
+ description: "The matching accounts that have used this app",
13
+ minItems: 0
14
+ }
15
+
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -4,11 +4,11 @@ module OpenStax
4
4
  module Accounts
5
5
  module Api
6
6
  module V1
7
- class ApplicationUsersRepresenter < Roar::Decorator
7
+ class ApplicationAccountsRepresenter < Roar::Decorator
8
8
  include Representable::JSON::Collection
9
9
 
10
- items class: OpenStax::Accounts::ApplicationUser,
11
- decorator: ApplicationUserRepresenter
10
+ items class: OpenStax::Accounts::ApplicationAccount,
11
+ decorator: ApplicationAccountRepresenter
12
12
  end
13
13
  end
14
14
  end
@@ -1,10 +1,10 @@
1
- # Routine for searching for users
1
+ # Routine for searching for accounts
2
2
  #
3
3
  # Caller provides a query and some options. The query follows the rules of
4
4
  # https://github.com/bruce/keyword_search, e.g.:
5
5
  #
6
- # "username:jps,richb" --> returns the "jps" and "richb" users
7
- # "name:John" --> returns Users with first, last, or full name starting with "John"
6
+ # "username:jps,richb" --> returns the "jps" and "richb" accounts
7
+ # "name:John" --> returns accounts with first, last, or full name starting with "John"
8
8
  #
9
9
  # Query terms can be combined, e.g. "username:jp first_name:john"
10
10
  #
@@ -16,9 +16,9 @@
16
16
  module OpenStax
17
17
  module Accounts
18
18
  module Dev
19
- class SearchUsers < OpenStax::Accounts::SearchUsers
19
+ class SearchAccounts < OpenStax::Accounts::SearchAccounts
20
20
  def exec(query, options={})
21
- options = options.merge!(:max_matching_users => Float::INFINITY)
21
+ options = options.merge!(:max_matching_accounts => Float::INFINITY)
22
22
  super
23
23
  end
24
24
  end