Created
January 7, 2014 21:18
-
-
Save jboursiquot/8307082 to your computer and use it in GitHub Desktop.
app/models/authentication/attempt.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Authentication | |
class Attempt | |
class_attribute :bypass_team_check | |
attr_reader :data | |
def initialize(auth_data, user_params = {}) | |
@data = auth_data | |
@user_params = user_params | |
end | |
def valid? | |
self.bypass_team_check || team_check.passes? | |
end | |
def user | |
user = User.from_authentication_attempt(self).tap do |u| | |
u.attributes = @user_params | |
end | |
end | |
def session_hash | |
@session_hash = @data.dup.tap do |hsh| | |
hsh.delete('extra') | |
hsh['role'] = role | |
end | |
end | |
def effective_teams | |
if self.bypass_team_check | |
[] | |
else | |
team_check.effective_teams | |
end | |
end | |
def role | |
if self.bypass_team_check | |
nil | |
else | |
if admin? | |
'admin' | |
elsif experience_engineer? | |
'experience_engineer' | |
else | |
'user' | |
end | |
end | |
end | |
def provider | |
@data['provider'] | |
end | |
def login | |
@data['info'].try(:[], 'nickname') | |
end | |
protected | |
def team_check | |
@team_check ||= Authentication::TeamCheck.new(token, login) | |
end | |
def admin? | |
team_check.admin? | |
end | |
def experience_engineer? | |
team_check.experience_engineer? | |
end | |
def token | |
@data['credentials'].try(:[], 'token') | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment