Class | User
In: | app/models/user.rb
Parent: | ActiveRecord::Base

More information:

- EPF Wiki Data model

Copyright (c) 2006 Logica

### Methods

admin? after_create before_save before_validation_on_update cadmin cadmin? change_password confirm_account documents_path find_central_admin images_path login login_basicauthentication login_bugzilla login_validemail new_cadmin new_signup set_new_pw sites token token_new try_to_login user? validate validate_on_create validate_on_update

### Constants

DOMAIN_PATTERN | = | /@.*/

### Attributes

email_extension | [RW] |
password | [RW] |
remember_me | [RW] |
user | [RW] | Changing account to admin or cadmin requires that you specify the user that is requesting the change

### Public Class methods

cadmin(from, to)

[Source]

         # File app/models/user.rb, line 234
    234:     def self.cadmin(from, to)
    235:       raise 'From needs to be central admin' if !from.cadmin?
    236:       User.transaction do
    237:         to.admin = 'C'
    238:         to.user = from
    239:         from.admin = 'Y'
    240:         to.save
    241:         from.save
    242:       end
    243:     end

find_central_admin()

[Source]

         # File app/models/user.rb, line 267
    267:     def self.find_central_admin
    268:         return  User.find(:first, :conditions => ["admin=?", "C"] )
    269:     end

login(email, password)

login searches the user on email and hashed_password and returns it, see also try_to_login

[Source]

        # File app/models/user.rb, line 74
    74:     def self.login(email, password)
    75:       user = nil
    76:       ENV['EPFWIKI_AUTH_METHODS'].split(',').each do |method|
    77:         logger.info("Doing login of #{email} using method #{method}")
    78:         if method == 'bugzilla' #&& user.nil?
    79:           user = User.login_bugzilla(email, password)
    80:         elsif method == 'validemail' #&& user.nil?
    81:           user = User.login_validemail(email, password)
    82:         elsif method == 'basic' #&& user.nil?
    83:           user = User.login_basicauthentication(email, password)
    84:         end
    85:         break if !user.nil?
    86:       end
    87:       return user
    88:     end

login_basicauthentication(account, password)

[Source]

         # File app/models/user.rb, line 142
    142:     def self.login_basicauthentication(account, password)
    143:       logger.info("Checking un/pw using basic authentication")
    144:       user = nil
    145:       hostname, fail_code, maildomain = ENV['EPFWIKI_AUTH_BASIC'].split(',')
    146:       logger.debug("BASIC AUTH Settings: #{hostname},#{fail_code},#{maildomain}")
    147:       Net::HTTP.start(hostname) {|http|
    148:         req = Net::HTTP::Get.new('/')
    149:         req.basic_auth account, password
    150:         response = http.request(req)
    151:         logger.debug("response.code: #{response.code.inspect}, fail_code #{fail_code.inspect}")
    152:         if response.code == fail_code
    153:           logger.debug("Unauthorized #{account}/#{password}: #{response.inspect}")
    154:           return nil
    155:         else
    156:           logger.debug("Authorized #{account}/#{password}: #{response.inspect}")
    157:           user = User.find_by_account(account)
    158:           if user
    159:             logger.info("User #{account} has account")
    160:           else
    161:             logger.info("Creating account #{account}")
    162:             user = User.new(:account => account, :email => "#{account}@#{maildomain}", :name => account)
    163:             user.set_new_pw
    164:             user.password_confirmation = user.password
    165:             user.hashed_password = hash_pw(user.password) if user.password
    166:             if user.save
    167:               logger.info("Succesfully created account: #{user.inspect}")
    168:             else
    169:               logger.info("Failed to create account #{user.errors.full_messages.join(", ")}")
    170:               Notifier::deliver_email(User.find_central_admin,
    171:               "[#{ENV['EPFWIKI_APP_NAME']}] Error creating account using basic authentication!",[],
    172:               "#{user.errors.full_messages.join(", ")}")
    173:               user = nil
    174:             end
    175:             #return User.create() if user.nil
    176:           end
    177:         end
    178:       }
    179:       return user
    180:     end

login_bugzilla(email, password)

[Source]

         # File app/models/user.rb, line 90
     90:     def self.login_bugzilla(email, password)
     91:       user = nil
     92:       host, port = ENV['EPFWIKI_AUTH_BUGZILLA'].split(',')
     93:       logger.debug("Login using bugzilla with settings: #{host} with port #{port}")
     94:       http = Net::HTTP.new(host, port)
     95:
     96:       # avoid console message "peer certificate won't be verified in this SSL session"
     97:       http.verify_mode = OpenSSL::SSL::VERIFY_NONE
     98:
     99:       http.use_ssl = true
    100:       path = '/bugs/index.cgi'
    101:
    102:       # POST request -> logging in
    103:       data = "Bugzilla_login=#{email}&Bugzilla_password=#{password}&GoAheadAndLogIn=1"
    104:       logger.debug('data = ' + data)
    105:       headers = {
    106:       'Referer' => "https://#{host}/bugs/index.cgi?GoAheadAndLogIn=",
    107:       'Content-Type' => 'application/x-www-form-urlencoded'
    108:       }
    109:
    110:       resp, data = http.post(path, data, headers)
    111:       logger.info('Code = ' + resp.code)
    112:       logger.info('Message = ' + resp.message)
    113:       resp.each {|key, val| logger.info(key + ' = ' + val)}
    114:
    115:       if resp['set-cookie'].nil?
    116:         logger.info("Unauthorized (didn't get a cookie)")
    117:       else
    118:           logger.debug("Authorized #{email}/#{password}")
    119:           user = User.find_by_email(email)
    120:           if user
    121:             logger.info("User #{email} has account")
    122:           else
    123:             logger.info("Creating account #{email}")
    124:             user = User.new(:email => email, :name => email.split('@')[0])
    125:             user.set_new_pw
    126:             user.password_confirmation = user.password
    127:             user.hashed_password = hash_pw(user.password) if user.password
    128:             if user.save
    129:               logger.info("Succesfully created account: #{user.inspect}")
    130:             else
    131:               logger.info("Failed to create account #{user.errors.full_messages.join(", ")}")
    132:               Notifier::deliver_email(User.find_central_admin,
    133:               "[#{ENV['EPFWIKI_APP_NAME']}] Error creating account using bugzilla!",[],
    134:               "#{user.errors.full_messages.join(", ")}")
    135:               user = nil
    136:             end
    137:           end
    138:         end
    139:       return user
    140:     end

login_validemail(email, password)

[Source]

         # File app/models/user.rb, line 182
    182:     def self.login_validemail(email, password)
    183:       logger.info("Checking un/pw of valid email #{email} hash_pw is #{hash_pw(password)}")
    184:         hashed_password = hash_pw(password)
    185:         user = find(:first,  :conditions => ["email = ? and hashed_password = ?", email.downcase, hashed_password])
    186:         return nil if user && (password.nil? ||  user.confirmed_on.nil?)
    187:         return user
    188:     end

new_cadmin(params)

new_cadmin creates the central adminstrator user

[Source]

        # File app/models/user.rb, line 55
    55:     def self.new_cadmin(params)
    56:       raise 'Already create central admin' if User.count > 0
    57:       u= User.new(params)
    58:       u.hashed_password = hash_pw(u.password) if u.password
    59:       u.admin = "C"
    60:       u.confirmed_on = Time.now
    61:       return u
    62:     end

new_signup(params)

new_signup creates an ordinary user account

[Source]

        # File app/models/user.rb, line 65
    65:     def self.new_signup(params)
    66:       user = User.new(params)
    67:       user.email = user.email + user.email_extension if ENV['EPFWIKI_DOMAINS'] && user.email_extension
    68:       logger.info("Creating account with supplied password for #{user.email}")
    69:       user.hashed_password = hash_pw(user.password) if user.password
    70:       return user
    71:     end

### Public Instance methods

admin?()

[Source]

         # File app/models/user.rb, line 259
    259:     def admin?
    260:         return admin == 'Y' || admin == 'C'
    261:     end

after_create()

[Source]

         # File app/models/user.rb, line 318
    318:     def after_create
    319:       create_templates if User.count == 1
    320:     end

before_save()

[Source]

         # File app/models/user.rb, line 322
    322:     def before_save
    323:       self.email = self.email.downcase
    324:     end

before_validation_on_update()

[Source]

         # File app/models/user.rb, line 284
    284:     def before_validation_on_update
    285:     end

cadmin?()

[Source]

         # File app/models/user.rb, line 263
    263:     def cadmin?
    264:         return admin  == 'C'
    265:     end

change_password(user)

change_password changes the password of a User

[Source]

         # File app/models/user.rb, line 226
    226:     def change_password(user)
    227:       raise "Password can't be blank" if user.password.blank?
    228:       self.password = user.password
    229:       self.password_confirmation = user.password_confirmation
    230:       self.hashed_password = hash_pw(user.password)
    231:       self.confirmed_on = Time.now
    232:     end

confirm_account(token)

confirm_account is used to confirm new accounts or confirm new passwords in case user requested on

[Source]

         # File app/models/user.rb, line 191
    191:     def confirm_account(token)
    192:       logger.debug("Confirming account with token: " + token)
    193:       logger.debug("Hashed password is: " + self.hashed_password)
    194:       logger.debug("Hashed password new is: " + (self.hashed_password_new || ''))
    195:       if  self.hashed_password && (hash_pw(self.hashed_password) == token)
    196:           logger.debug('Confirming new account:' + self.inspect)
    197:           self.confirmed_on = Time.now
    198:           return true
    199:       elsif self.hashed_password_new && (hash_pw(self.hashed_password_new) == token)
    200:           logger.debug('Confirming a lost password:' + self.inspect)
    201:           self.confirmed_on = Time.now
    202:           self.hashed_password = self.hashed_password_new
    203:           self.hashed_password_new = nil
    204:           return true
    205:       else
    206:         return false
    207:       end
    208:     end

documents_path()

[Source]

         # File app/models/user.rb, line 271
    271:     def documents_path
    272:         return "users/" + id.to_s + "/docs"
    273:     end

images_path()

[Source]

         # File app/models/user.rb, line 275
    275:     def images_path
    276:         return  "users/" + id.to_s + "/images"
    277:     end

set_new_pw(new_pw)

Use set_new_pw to set and return a new password for a user. Needs to be confirmed using confirm_account

[Source]

         # File app/models/user.rb, line 212
    212:     def set_new_pw(new_pw)
    213:         self.password = new_pw
    214:         self.hashed_password_new = hash_pw(new_pw)
    215:         logger.debug("This is the new password #{new_pw}")
    216:     end

sites()

sites returns Site records where user created versions or comments

[Source]

         # File app/models/user.rb, line 280
    280:     def sites
    281:         return Site.find(:all, :conditions => ['exists (select * from versions where user_id = ? and wiki_id = sites.id) or exists (select * from da_texts where user_id = ? and site_id = sites.id)', id, id])
    282:     end

token()

Token that can be used to confirm a new account

[Source]

         # File app/models/user.rb, line 246
    246:     def token
    247:       return hash_pw(self.hashed_password)
    248:     end

token_new()

Token that can be used to confirm a lost password (existing account)

[Source]

         # File app/models/user.rb, line 251
    251:     def token_new
    252:       return hash_pw(self.hashed_password_new)
    253:     end

try_to_login()

Log in if the name and password (after hashing) match the database, or if the name matches an entry in the database with no password

[Source]

         # File app/models/user.rb, line 221
    221:     def try_to_login
    222:         User.login(self.email.downcase, self.password)
    223:     end

user?()

[Source]

         # File app/models/user.rb, line 255
    255:     def user?
    256:       return admin == 'N'
    257:     end

validate()

[Source]

         # File app/models/user.rb, line 287
    287:     def validate
    288:       if  ENV['EPFWIKI_DOMAINS']
    289:         valid_domain = !ENV['EPFWIKI_DOMAINS'].split(" ").index(DOMAIN_PATTERN.match(email.downcase).to_s).nil?
    290:         errors.add(:email, "domain not valid") if !valid_domain && !self.cadmin?
    291:       end
    292:     end

validate_on_create()

[Source]

         # File app/models/user.rb, line 294
    294:     def validate_on_create
    295:       errors.add(:password, "can't be blank") if password.blank? || hashed_password.blank?
    296:       errors.add(:password_confirmation, "can't be blank") if password_confirmation.blank?
    297:       errors.add("Central admin already exists") if User.count > 0 && admin == 'C'
    298:       # all users have to agree to the terms of use (except the first user)
    299:       # errors.add_to_base("You have to agree to the terms of use") if i_agree_to_the_terms_of_use != "1" && User.count != 0
    300:     end

validate_on_update()

[Source]

         # File app/models/user.rb, line 302
    302:     def validate_on_update
    303:       errors.add(:hashed_password, "can't be blank") if hashed_password.blank?
    304:       old_admin = User.find(id).admin
    305:       if admin == 'C' and old_admin != 'C'
    306:         if user.nil? || User.find(user.id).admin != 'C'
    307:           errors.add(:admin, 'can only be set to C by the central admin')
    308:         end
    309:       end
    310:       if admin == 'Y' and old_admin == 'N'
    311:         errors.add(:admin, 'can only be set by an admin') if user.nil? || user.admin == 'N'
    312:       end
    313:       if admin == 'N' and !old_admin.index(/Y|C/).nil?
    314:         errors.add(:admin, 'can only be revoked by the central admin') if user.nil? || user.admin != 'C'
    315:       end
    316:     end

[Validate]