2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-10-03 17:38:42 +01:00
|
|
|
class User < Avatar
|
2012-12-06 18:04:39 +00:00
|
|
|
ROLES = ['', 'admin', 'banned', 'tester']
|
2012-01-17 11:57:46 +00:00
|
|
|
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
|
2012-01-18 13:06:18 +00:00
|
|
|
LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last)
|
2011-10-23 22:39:44 +01:00
|
|
|
|
2012-04-18 13:46:09 +01:00
|
|
|
devise :database_authenticatable, :registerable, :omniauthable, :token_authenticatable,# :encryptable, :timeoutable
|
2012-03-26 15:33:02 +01:00
|
|
|
:recoverable, :rememberable, :validatable, :lockable, :confirmable#, :reconfirmable, :trackable
|
2012-03-03 18:05:02 +00:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
has_one :notifier, :class_name => 'SettingsNotifier', :dependent => :destroy #:notifier
|
2012-01-11 13:58:13 +00:00
|
|
|
|
2012-03-06 15:53:04 +00:00
|
|
|
has_many :activity_feeds, :dependent => :destroy
|
2012-01-25 08:31:49 +00:00
|
|
|
|
2011-10-21 18:17:49 +01:00
|
|
|
has_many :authentications, :dependent => :destroy
|
2011-12-21 21:42:06 +00:00
|
|
|
has_many :build_lists, :dependent => :destroy
|
2012-02-20 23:13:05 +00:00
|
|
|
has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy
|
|
|
|
has_many :comments, :dependent => :destroy
|
2011-10-21 18:17:49 +01:00
|
|
|
|
2012-04-26 02:38:33 +01:00
|
|
|
has_many :relations, :as => :actor, :dependent => :destroy
|
|
|
|
has_many :targets, :as => :actor, :class_name => 'Relation', :dependent => :destroy
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-10-17 15:23:51 +01:00
|
|
|
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
|
2012-02-20 23:13:05 +00:00
|
|
|
has_many :groups, :through => :targets, :source => :target, :source_type => 'Group', :autosave => true
|
2011-10-17 15:23:51 +01:00
|
|
|
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2012-02-20 23:13:05 +00:00
|
|
|
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
|
|
|
|
has_many :own_groups, :foreign_key => :owner_id, :class_name => 'Group', :dependent => :destroy
|
|
|
|
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2012-07-17 10:31:32 +01:00
|
|
|
has_many :key_pairs
|
|
|
|
|
2012-11-07 10:20:24 +00:00
|
|
|
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => {:with => /\A[a-z0-9_]+\z/}, :reserved_name => true
|
2012-05-02 10:18:07 +01:00
|
|
|
validate { errors.add(:uname, :taken) if Group.by_uname(uname).present? }
|
2012-03-22 14:50:15 +00:00
|
|
|
validates :role, :inclusion => {:in => ROLES}, :allow_blank => true
|
2012-01-17 11:57:46 +00:00
|
|
|
validates :language, :inclusion => {:in => LANGUAGES}, :allow_blank => true
|
2011-03-10 21:48:15 +00:00
|
|
|
|
2012-03-26 15:49:00 +01:00
|
|
|
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :login, :name, :uname, :language,
|
2012-10-03 17:38:42 +01:00
|
|
|
:site, :company, :professional_experience, :location
|
2012-05-02 10:18:07 +01:00
|
|
|
attr_readonly :uname
|
2011-10-11 21:56:51 +01:00
|
|
|
attr_accessor :login
|
|
|
|
|
2012-03-28 00:58:03 +01:00
|
|
|
scope :opened, where('1=1')
|
2012-03-22 14:50:15 +00:00
|
|
|
scope :banned, where(:role => 'banned')
|
|
|
|
scope :admin, where(:role => 'admin')
|
2012-12-06 18:04:39 +00:00
|
|
|
scope :tester, where(:role => 'tester')
|
2012-03-22 14:50:15 +00:00
|
|
|
scope :real, where(:role => ['', nil])
|
2012-03-06 15:53:04 +00:00
|
|
|
|
2012-08-24 16:19:26 +01:00
|
|
|
scope :member_of_project, lambda {|item|
|
|
|
|
where "#{table_name}.id IN (?)", item.members.map(&:id).uniq
|
|
|
|
}
|
|
|
|
|
2012-03-06 15:53:04 +00:00
|
|
|
after_create lambda { self.create_notifier }
|
2012-04-18 13:46:09 +01:00
|
|
|
before_create :ensure_authentication_token
|
2012-01-11 13:58:13 +00:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
include Modules::Models::PersonalRepository
|
|
|
|
include Modules::Models::ActsLikeMember
|
2012-04-19 20:45:50 +01:00
|
|
|
|
2011-11-15 20:05:08 +00:00
|
|
|
def admin?
|
2011-11-16 18:45:01 +00:00
|
|
|
role == 'admin'
|
2011-11-15 20:05:08 +00:00
|
|
|
end
|
2012-01-26 08:53:40 +00:00
|
|
|
|
2012-03-01 17:33:46 +00:00
|
|
|
def user?
|
|
|
|
persisted?
|
|
|
|
end
|
|
|
|
|
2011-11-15 20:05:08 +00:00
|
|
|
def guest?
|
2012-03-01 17:33:46 +00:00
|
|
|
new_record?
|
2011-11-15 20:05:08 +00:00
|
|
|
end
|
|
|
|
|
2012-12-06 18:04:39 +00:00
|
|
|
def tester?
|
|
|
|
role == 'tester'
|
|
|
|
end
|
|
|
|
|
2012-03-22 14:50:15 +00:00
|
|
|
def access_locked?
|
2012-05-02 10:18:07 +01:00
|
|
|
role == 'banned'
|
2012-03-22 14:50:15 +00:00
|
|
|
end
|
|
|
|
|
2011-12-20 17:09:29 +00:00
|
|
|
def fullname
|
2012-07-04 16:53:18 +01:00
|
|
|
return name.present? && name.length > 0 ? "#{uname} (#{name})" : uname
|
2011-12-20 17:09:29 +00:00
|
|
|
end
|
2012-03-06 15:53:04 +00:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
def user_appeal
|
|
|
|
name.presence || uname
|
|
|
|
end
|
|
|
|
|
2011-10-11 21:56:51 +01:00
|
|
|
class << self
|
|
|
|
def find_for_database_authentication(warden_conditions)
|
|
|
|
conditions = warden_conditions.dup
|
|
|
|
login = conditions.delete(:login)
|
2012-12-25 15:55:56 +00:00
|
|
|
where(conditions)
|
|
|
|
.where(["lower(uname) = :value OR lower(email) = :value OR authentication_token = :orig_value",
|
|
|
|
{ :value => login.downcase, :orig_value => login }]).first
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_with_session(params, session)
|
|
|
|
super.tap do |user|
|
|
|
|
if data = session["devise.omniauth_data"]
|
2011-12-06 19:57:03 +00:00
|
|
|
if info = data['info'] and info.present?
|
2011-10-13 11:19:42 +01:00
|
|
|
user.email = info['email'].presence if user.email.blank?
|
2011-10-21 18:17:49 +01:00
|
|
|
user.uname ||= info['nickname'].presence || info['username'].presence
|
2011-10-11 21:56:51 +01:00
|
|
|
user.name ||= info['name'].presence || [info['first_name'], info['last_name']].join(' ').strip
|
|
|
|
end
|
|
|
|
user.password = Devise.friendly_token[0,20] # stub password
|
|
|
|
user.authentications.build :uid => data['uid'], :provider => data['provider']
|
|
|
|
end
|
2011-03-29 23:16:04 +01:00
|
|
|
end
|
2011-03-10 21:48:15 +00:00
|
|
|
end
|
2012-12-25 15:55:56 +00:00
|
|
|
|
|
|
|
def auth_by_token_or_login_pass(user, pass)
|
|
|
|
u = User.find_for_database_authentication(:login => user)
|
|
|
|
u if u && !u.access_locked? && (u.authentication_token == user || u.valid_password?(pass))
|
|
|
|
end
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
2011-03-10 21:48:15 +00:00
|
|
|
|
2012-03-05 15:36:23 +00:00
|
|
|
# def update_with_password(params={})
|
|
|
|
# params.delete(:current_password)
|
|
|
|
# # self.update_without_password(params) # Don't allow password update
|
|
|
|
# if params[:password].blank?
|
|
|
|
# params.delete(:password)
|
|
|
|
# params.delete(:password_confirmation) if params[:password_confirmation].blank?
|
|
|
|
# end
|
|
|
|
# result = update_attributes(params)
|
|
|
|
# clean_up_passwords
|
|
|
|
# result
|
|
|
|
# end
|
2012-01-26 08:53:40 +00:00
|
|
|
|
2012-01-29 20:18:14 +00:00
|
|
|
def commentor?(commentable)
|
2012-02-14 20:28:01 +00:00
|
|
|
comments.exists?(:commentable_type => commentable.class.name, :commentable_id => commentable.id.hex)
|
2012-01-29 20:18:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def committer?(commit)
|
2012-01-30 06:38:23 +00:00
|
|
|
email.downcase == commit.committer.email.downcase
|
2012-01-29 20:18:14 +00:00
|
|
|
end
|
|
|
|
|
2012-03-11 23:08:50 +00:00
|
|
|
def owner_of? object
|
|
|
|
if object.respond_to? :owner
|
|
|
|
object.owner_id == self.id or self.group_ids.include? object.owner_id
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2012-06-19 19:24:35 +01:00
|
|
|
|
2012-06-21 10:49:39 +01:00
|
|
|
def best_role target
|
|
|
|
roles = target_roles(target)
|
|
|
|
return nil if roles.count == 0
|
2012-06-25 15:31:41 +01:00
|
|
|
%w(admin writer reader).each {|role| return role if roles.include?(role)}
|
2012-06-21 10:49:39 +01:00
|
|
|
raise "unknown user #{self.uname} roles #{roles}"
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2012-06-20 19:10:27 +01:00
|
|
|
def target_roles target
|
|
|
|
rel, gr, roles = target.relations, self.groups, []
|
|
|
|
|
2012-06-21 15:19:44 +01:00
|
|
|
if target.owner.class == Group
|
2012-06-19 19:24:35 +01:00
|
|
|
owner_group = self.groups.where(:id => target.owner.id).first
|
2012-06-20 19:10:27 +01:00
|
|
|
roles += owner_group.actors.where(:actor_id => self) if owner_group# user group is owner
|
2012-06-21 15:19:44 +01:00
|
|
|
|
|
|
|
gr = gr.where('groups.id != ?', target.owner.id) # exclude target owner group from users group list
|
2012-06-19 19:24:35 +01:00
|
|
|
end
|
|
|
|
roles += rel.where(:actor_id => self.id, :actor_type => 'User') # user is member
|
|
|
|
roles += rel.where(:actor_id => gr, :actor_type => 'Group') # user group is member
|
2012-06-20 19:10:27 +01:00
|
|
|
roles.map(&:role).uniq
|
|
|
|
end
|
|
|
|
|
2011-03-09 16:16:48 +00:00
|
|
|
end
|