Class: Locomotive::Account
- Inherits:
-
Object
- Object
- Locomotive::Account
- Includes:
- Mongoid::Document
- Defined in:
- app/models/locomotive/account.rb
Class Method Summary collapse
-
.create_api_token(site, email, password) ⇒ String
Create the API token which will be passed to all the requests to the Locomotive API.
-
.invalidate_api_token(token) ⇒ String
Logout the user responding to the token passed in parameter from the API.
Instance Method Summary collapse
-
#admin? ⇒ Boolean
Tell if the account has admin privileges or not.
- #devise_mailer ⇒ Object
-
#name ⇒ Object
validations ##.
-
#remember_created_at ⇒ Object
devise fields (need to be declared since 2.x) ##.
-
#sites ⇒ Object
methods ##.
Class Method Details
.create_api_token(site, email, password) ⇒ String
Create the API token which will be passed to all the requests to the Locomotive API. It requires the credentials of an account with admin role. If an error occurs (invalid account, …etc), this method raises an exception that has to be caught somewhere.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/locomotive/account.rb', line 61 def self.create_api_token(site, email, password) raise 'The request must contain the user email and password.' if email.blank? or password.blank? account = self.where(:email => email.downcase).first raise 'Invalid email or password.' if account.nil? account.ensure_authentication_token! if not account.valid_password?(password) # TODO: check admin roles raise 'Invalid email or password.' end account.authentication_token end |
.invalidate_api_token(token) ⇒ String
Logout the user responding to the token passed in parameter from the API. An exception is raised if no account corresponds to the token.
84 85 86 87 88 89 90 91 92 |
# File 'app/models/locomotive/account.rb', line 84 def self.invalidate_api_token(token) account = self.where(:authentication_token => token).first raise 'Invalid token.' if account.nil? account.reset_authentication_token! token end |
Instance Method Details
#admin? ⇒ Boolean
Tell if the account has admin privileges or not. Actually, an account is considered as an admin if it owns at least one admin membership in all its sites.
46 47 48 |
# File 'app/models/locomotive/account.rb', line 46 def admin? Site.where(:memberships => { '$elemMatch' => { :account_id => self._id, :role => :admin } }).count > 0 end |
#devise_mailer ⇒ Object
94 95 96 |
# File 'app/models/locomotive/account.rb', line 94 def devise_mailer Locomotive::DeviseMailer end |
#name ⇒ Object
validations ##
23 |
# File 'app/models/locomotive/account.rb', line 23 field :name |
#remember_created_at ⇒ Object
devise fields (need to be declared since 2.x) ##
9 |
# File 'app/models/locomotive/account.rb', line 9 field :remember_created_at, :type => Time |
#sites ⇒ Object
methods ##
36 37 38 |
# File 'app/models/locomotive/account.rb', line 36 def sites @sites ||= Site.where('memberships.account_id' => self._id) end |