2011-03-09 16:16:48 +00:00
|
|
|
class User < ActiveRecord::Base
|
2011-10-23 22:39:44 +01:00
|
|
|
relationable :as => :object
|
|
|
|
inherit_rights_from :groups
|
|
|
|
|
2011-10-26 10:31:58 +01:00
|
|
|
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
|
2011-10-11 21:56:51 +01:00
|
|
|
:recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable
|
2011-10-21 18:17:49 +01:00
|
|
|
|
|
|
|
has_many :authentications, :dependent => :destroy
|
|
|
|
|
2011-10-23 22:39:44 +01:00
|
|
|
belongs_to :global_role, :class_name => 'Role'
|
2011-10-19 21:19:45 +01:00
|
|
|
has_many :roles, :through => :targets
|
2011-10-21 18:17:49 +01:00
|
|
|
|
2011-10-13 16:55:03 +01:00
|
|
|
has_many :targets, :as => :object, :class_name => 'Relation'
|
|
|
|
|
2011-11-01 22:33:20 +00:00
|
|
|
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
|
2011-10-18 16:00:06 +01:00
|
|
|
has_many :own_groups, :foreign_key => :owner_id, :class_name => 'Group'
|
2011-11-01 22:33:20 +00:00
|
|
|
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
|
|
|
|
has_many :own_repositories, :as => :owner, :class_name => 'Repository', :dependent => :destroy
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2011-10-17 15:23:51 +01:00
|
|
|
has_many :groups, :through => :targets, :source => :target, :source_type => 'Group', :autosave => true
|
|
|
|
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
|
|
|
|
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true
|
|
|
|
has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-10-27 14:04:03 +01:00
|
|
|
include PersonalRepository
|
|
|
|
|
2011-10-20 15:05:06 +01:00
|
|
|
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-zA-Z0-9_]+$/ }, :allow_nil => false, :allow_blank => false
|
2011-10-27 19:13:55 +01:00
|
|
|
validates :ssh_key, :uniqueness => true, :allow_blank => true
|
2011-10-26 21:57:51 +01:00
|
|
|
validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? }
|
2011-10-27 14:04:03 +01:00
|
|
|
#TODO: Replace this simple cross-table uniq validation by more progressive analog
|
|
|
|
validate lambda {
|
|
|
|
errors.add(:uname, I18n.t('flash.user.group_uname_exists')) if Group.exists? :uname => uname
|
|
|
|
}
|
2011-03-10 21:48:15 +00:00
|
|
|
|
2011-10-29 15:14:41 +01:00
|
|
|
attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :name, :ssh_key, :uname
|
2011-10-20 15:05:06 +01:00
|
|
|
attr_readonly :uname
|
2011-10-11 21:56:51 +01:00
|
|
|
attr_accessor :login
|
|
|
|
|
2011-10-31 00:33:47 +00:00
|
|
|
before_create :add_default_role
|
2011-10-28 14:10:29 +01:00
|
|
|
|
2011-10-26 21:57:51 +01:00
|
|
|
before_update {
|
|
|
|
if ssh_key_was.blank? and ssh_key.present?
|
|
|
|
create_ssh_key ssh_key
|
|
|
|
elsif ssh_key_was.present? and ssh_key.blank?
|
|
|
|
destroy_ssh_key ssh_key_was
|
2011-10-29 14:21:04 +01:00
|
|
|
elsif ssh_key_changed? and ssh_key.present? and ssh_key_was.present?
|
2011-10-26 21:57:51 +01:00
|
|
|
update_ssh_key ssh_key_was, ssh_key
|
|
|
|
end
|
|
|
|
}
|
2011-10-27 22:53:15 +01:00
|
|
|
before_destroy { destroy_ssh_key(ssh_key) if ssh_key.present? }
|
2011-10-11 21:56:51 +01:00
|
|
|
# after_create() { UserMailer.new_user_notification(self).deliver }
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def find_for_database_authentication(warden_conditions)
|
|
|
|
conditions = warden_conditions.dup
|
|
|
|
login = conditions.delete(:login)
|
2011-10-20 15:05:06 +01:00
|
|
|
where(conditions).where(["lower(uname) = :value OR lower(email) = :value", { :value => login.downcase }]).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"]
|
|
|
|
if info = data['user_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
|
2011-10-11 21:56:51 +01:00
|
|
|
end
|
2011-03-10 21:48:15 +00:00
|
|
|
|
2011-10-11 21:56:51 +01: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?
|
2011-03-10 21:48:15 +00:00
|
|
|
end
|
2011-10-11 21:56:51 +01:00
|
|
|
result = update_attributes(params)
|
|
|
|
clean_up_passwords
|
|
|
|
result
|
|
|
|
end
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2011-10-21 18:17:49 +01:00
|
|
|
protected
|
2011-10-26 21:57:51 +01:00
|
|
|
|
|
|
|
def create_ssh_key(key)
|
|
|
|
with_ga do |ga|
|
|
|
|
ga.store_key! key
|
2011-11-01 00:52:24 +00:00
|
|
|
projects.each do |project|
|
2011-10-26 21:57:51 +01:00
|
|
|
repo = ga.find_repo(project.git_repo_name)
|
|
|
|
repo.add_key(key, 'RW') if repo
|
|
|
|
end
|
|
|
|
ga.save_and_release
|
|
|
|
end
|
2011-10-18 16:00:06 +01:00
|
|
|
end
|
|
|
|
|
2011-10-26 21:57:51 +01:00
|
|
|
def update_ssh_key(old_key, new_key)
|
|
|
|
with_ga do |ga|
|
2011-11-01 00:52:24 +00:00
|
|
|
ga.repos.replace_key old_key, new_key #, options = {}
|
2011-10-26 21:57:51 +01:00
|
|
|
ga.replace_key! old_key, new_key
|
|
|
|
ga.save_and_release
|
|
|
|
end
|
2011-10-18 16:00:06 +01:00
|
|
|
end
|
|
|
|
|
2011-10-26 21:57:51 +01:00
|
|
|
def destroy_ssh_key(key)
|
|
|
|
with_ga do |ga|
|
|
|
|
ga.repos.rm_key key
|
|
|
|
ga.rm_key! key
|
|
|
|
ga.save_and_release
|
|
|
|
end
|
2011-10-18 16:00:06 +01:00
|
|
|
end
|
2011-03-09 16:16:48 +00:00
|
|
|
end
|