rosa-build/app/models/user.rb

177 lines
6.1 KiB
Ruby
Raw Normal View History

class User < Avatar
2012-12-06 18:04:39 +00:00
ROLES = ['', 'admin', 'banned', 'tester']
2012-12-27 18:27:50 +00:00
EXTENDED_ROLES = ROLES | ['system']
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last)
2011-10-23 22:39:44 +01:00
devise :database_authenticatable, :registerable, :omniauthable, :token_authenticatable,# :encryptable, :timeoutable
:recoverable, :rememberable, :validatable, :lockable, :confirmable#, :reconfirmable, :trackable
devise :omniauthable, :omniauth_providers => [:facebook, :google_oauth2, :github]
has_one :notifier, :class_name => 'SettingsNotifier', :dependent => :destroy #:notifier
2012-01-11 13:58:13 +00:00
has_many :activity_feeds, :dependent => :destroy
has_many :authentications, :dependent => :destroy
has_many :build_lists, :dependent => :destroy
has_many :subscribes, :foreign_key => :user_id, :dependent => :destroy
has_many :comments, :dependent => :destroy
has_many :relations, :as => :actor, :dependent => :destroy
has_many :targets, :as => :actor, :class_name => 'Relation', :dependent => :destroy
2011-10-17 15:23:51 +01:00
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
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
has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository'
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
2013-04-29 19:13:45 +01:00
has_many :issues
2013-04-17 21:53:17 +01:00
has_many :assigned_issues, :foreign_key => :assignee_id, :class_name => 'Issue', :dependent => :nullify
has_many :key_pairs
2013-03-01 11:33:02 +00:00
has_many :ssh_keys, :dependent => :destroy
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => {:with => /\A[a-z0-9_]+\z/}, :reserved_name => true
validate { errors.add(:uname, :taken) if Group.by_uname(uname).present? }
2012-12-27 18:27:50 +00:00
validates :role, :inclusion => {:in => EXTENDED_ROLES}, :allow_blank => true
validates :language, :inclusion => {:in => LANGUAGES}, :allow_blank => true
2011-03-10 21:48:15 +00:00
attr_accessible :email, :password, :password_confirmation, :current_password, :remember_me, :login, :name, :uname, :language,
:site, :company, :professional_experience, :location, :sound_notifications
attr_readonly :uname
attr_accessor :login
2012-12-29 14:49:28 +00:00
scope :opened, where('users.role != \'system\' OR users.role IS NULL')
2012-03-22 14:50:15 +00:00
scope :real, where(:role => ['', nil])
2012-12-28 19:24:41 +00:00
EXTENDED_ROLES.select {|type| type.present?}.each do |type|
scope type.to_sym, where(:role => type)
end
scope :member_of_project, lambda {|item|
where "#{table_name}.id IN (?)", item.members.map(&:id).uniq
}
2012-12-27 18:27:50 +00:00
after_create lambda { self.create_notifier unless self.system? }
before_create :ensure_authentication_token
2012-01-11 13:58:13 +00:00
include Modules::Models::PersonalRepository
include Modules::Models::ActsLikeMember
include Modules::Observers::ActivityFeed::User
def admin?
role == 'admin'
end
2012-01-26 08:53:40 +00:00
def user?
persisted?
end
def guest?
new_record?
end
2012-12-06 18:04:39 +00:00
def tester?
role == 'tester'
end
2012-12-27 18:27:50 +00:00
def system?
role == 'system'
end
2012-12-06 18:04:39 +00:00
2012-03-22 14:50:15 +00:00
def access_locked?
role == 'banned'
2012-03-22 14:50:15 +00:00
end
def fullname
return name.present? ? "#{uname} (#{name})" : uname
end
def user_appeal
name.presence || uname
end
class << self
def find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions)
.where(["lower(uname) = :value OR lower(email) = :value OR authentication_token = :orig_value",
{ :value => login.downcase, :orig_value => login }]).first
end
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
end
2011-03-10 21:48:15 +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)
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
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
return nil if target.nil?
2012-06-21 10:49:39 +01:00
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
2013-04-17 21:53:17 +01:00
def check_assigned_issues target
if target.is_a? Project
assigned_issues.where(:project_id => target.id).update_all(:assignee_id => nil)
else
ability = Ability.new self
project_ids = Project.accessible_by(ability, :membered).uniq.pluck(:id)
issues = assigned_issues
issues = issues.where('project_id not in (?)', project_ids) if project_ids.present?
issues.update_all(:assignee_id => nil)
end
end
2012-06-21 10:49:39 +01:00
protected
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
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
roles.map(&:role).uniq
end
2011-03-09 16:16:48 +00:00
end