Module: Card::Auth
- Defined in:
- lib/card/auth.rb
Constant Summary collapse
- NON_CREATEABLE_TYPES =
NEED API
%w{ signup setting set }- SETUP_COMPLETED_KEY =
'SETUP_COMPLETED'- @@as_card =
@@as_id = @@current_id = @@current = nil
- @@simulating_setup_need =
nil
Class Method Summary collapse
-
.[](email) ⇒ Object
find accounted by email.
- .always_ok? ⇒ Boolean
- .among?(authzed) ⇒ Boolean
- .as(given_user) ⇒ Object
- .as_bot(&block) ⇒ Object
- .as_card ⇒ Object
- .as_id ⇒ Object
-
.authenticate(email, password) ⇒ Object
Authenticates a user by their login name and unencrypted password.
-
.createable_types ⇒ Object
PERMISSIONS.
- .current ⇒ Object
- .current_id ⇒ Object
- .current_id=(card_id) ⇒ Object
-
.encrypt(password, salt) ⇒ Object
Encrypts some data with the salt.
- .get_user_id(user) ⇒ Object
- .instant_account_activation ⇒ Object
- .needs_setup? ⇒ Boolean
- .password_authenticated?(account, password) ⇒ Boolean
- .session ⇒ Object
- .set_current_from_session ⇒ Object
- .signed_in? ⇒ Boolean
- .signin(signin_id) ⇒ Object
- .simulate_setup_need!(mode = true) ⇒ Object
Class Method Details
.[](email) ⇒ Object
find accounted by email
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/card/auth.rb', line 35 def [] email Auth.as_bot do Card.search(right_plus: [ { id: Card::AccountID }, { right_plus: [ { id: Card::EmailID }, { content: email.strip.downcase } ] } ]).first end end |
.always_ok? ⇒ Boolean
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/card/auth.rb', line 148 def always_ok? # warn Rails.logger.warn("aok? #{as_id}, #{as_id&&Card[as_id].id}") return false unless (usr_id = as_id) return true if usr_id == Card::WagnBotID # cannot disable always = Card.cache.read('ALWAYS') || {} # warn(Rails.logger.warn "Auth.always_ok? #{usr_id}") if always[usr_id].nil? always = always.dup if always.frozen? always[usr_id] = !!Card[usr_id].all_roles.find { |r| r == Card::AdministratorID } # warn(Rails.logger.warn "update always hash #{always[usr_id]}, # #{always.inspect}") Card.cache.write 'ALWAYS', always end # warn Rails.logger.warn("aok? #{usr_id}, #{always[usr_id]}") always[usr_id] end |
.among?(authzed) ⇒ Boolean
109 110 111 |
# File 'lib/card/auth.rb', line 109 def among? authzed as_card.among? authzed end |
.as(given_user) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/card/auth.rb', line 92 def as given_user tmp_id, tmp_card = @@as_id, @@as_card # we could go ahead and set as_card if given a card... @@as_id, @@as_card = get_user_id(given_user), nil @@current_id = @@as_id if @@current_id.nil? return unless block_given? value = yield @@as_id, @@as_card = tmp_id, tmp_card value end |
.as_bot(&block) ⇒ Object
105 106 107 |
# File 'lib/card/auth.rb', line 105 def as_bot &block as Card::WagnBotID, &block end |
.as_card ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/card/auth.rb', line 117 def as_card if @@as_card && @@as_card.id == as_id @@as_card else @@as_card = Card[as_id] end end |
.as_id ⇒ Object
113 114 115 |
# File 'lib/card/auth.rb', line 113 def as_id @@as_id || current_id end |
.authenticate(email, password) ⇒ Object
Authenticates a user by their login name and unencrypted password.
15 16 17 18 19 20 21 22 23 |
# File 'lib/card/auth.rb', line 15 def authenticate email, password accounted = Auth[email] return unless accounted && (account = accounted.account) && account.active? if Card.config.no_authentication || password_authenticated?(account, password.strip) accounted.id end end |
.createable_types ⇒ Object
PERMISSIONS
168 169 170 171 172 173 174 175 176 |
# File 'lib/card/auth.rb', line 168 def createable_types type_names = Auth.as_bot do Card.search type: Card::CardtypeID, return: :name, not: { codename: ['in'] + NON_CREATEABLE_TYPES } end type_names.select do |name| Card.new(type: name).ok? :create end.sort end |
.current ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/card/auth.rb', line 71 def current if @@current && @@current.id == current_id @@current else @@current = Card[current_id] end end |
.current_id ⇒ Object
67 68 69 |
# File 'lib/card/auth.rb', line 67 def current_id @@current_id ||= Card::AnonymousID end |
.current_id=(card_id) ⇒ Object
79 80 81 82 |
# File 'lib/card/auth.rb', line 79 def current_id= card_id @@current = @@as_id = @@as_card = nil @@current_id = card_id end |
.encrypt(password, salt) ⇒ Object
Encrypts some data with the salt.
30 31 32 |
# File 'lib/card/auth.rb', line 30 def encrypt password, salt Digest::SHA1.hexdigest "#{salt}--#{password}--" end |
.get_user_id(user) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/card/auth.rb', line 84 def get_user_id user case user when NilClass then nil when Card then user.id else Card.fetch_id(user) end end |
.instant_account_activation ⇒ Object
141 142 143 144 145 146 |
# File 'lib/card/auth.rb', line 141 def instant_account_activation simulate_setup_need! yield ensure simulate_setup_need! false end |
.needs_setup? ⇒ Boolean
129 130 131 132 133 134 135 |
# File 'lib/card/auth.rb', line 129 def needs_setup? @@simulating_setup_need || ( !Card.cache.read(SETUP_COMPLETED_KEY) && !Card.cache.write(SETUP_COMPLETED_KEY, account_count > 2) ) # every deck starts with WagnBot and Anonymous account end |
.password_authenticated?(account, password) ⇒ Boolean
25 26 27 |
# File 'lib/card/auth.rb', line 25 def password_authenticated? account, password account.password == encrypt(password, account.salt) end |
.session ⇒ Object
51 52 53 |
# File 'lib/card/auth.rb', line 51 def session Card::Env[:session] end |
.set_current_from_session ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/card/auth.rb', line 55 def set_current_from_session self.current_id = if session if (card_id = session[:user]) && Card.exists?(card_id) card_id else session[:user] = nil end end current_id end |
.signed_in? ⇒ Boolean
125 126 127 |
# File 'lib/card/auth.rb', line 125 def signed_in? current_id != Card::AnonymousID end |
.signin(signin_id) ⇒ Object
46 47 48 49 |
# File 'lib/card/auth.rb', line 46 def signin signin_id self.current_id = signin_id session[:user] = signin_id if session end |
.simulate_setup_need!(mode = true) ⇒ Object
137 138 139 |
# File 'lib/card/auth.rb', line 137 def simulate_setup_need! mode=true @@simulating_setup_need = mode end |