user.rb 3.8 KB
Newer Older
G
gitlabhq 已提交
1 2 3
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
N
Nihad Abbasov 已提交
4
  devise :database_authenticatable, :token_authenticatable,
V
Valery Sizov 已提交
5
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable
G
gitlabhq 已提交
6 7

  # Setup accessible (or protected) attributes for your model
8
  attr_accessible :email, :password, :password_confirmation, :remember_me, :bio,
9
                  :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, :theme_id
G
gitlabhq 已提交
10 11 12

  has_many :users_projects, :dependent => :destroy
  has_many :projects, :through => :users_projects
V
Valera Sizov 已提交
13
  has_many :my_own_projects, :class_name => "Project", :foreign_key => :owner_id
G
gitlabhq 已提交
14
  has_many :keys, :dependent => :destroy
15 16 17 18 19 20

  has_many :recent_events, 
    :class_name => "Event",
    :foreign_key => :author_id,
    :order => "id DESC"

G
gitlabhq 已提交
21 22 23 24
  has_many :issues,
    :foreign_key => :author_id,
    :dependent => :destroy

25 26 27 28
  has_many :notes,
    :foreign_key => :author_id,
    :dependent => :destroy

G
gitlabhq 已提交
29 30 31 32 33
  has_many :assigned_issues,
    :class_name => "Issue",
    :foreign_key => :assignee_id,
    :dependent => :destroy

34 35 36 37 38 39 40 41 42
  has_many :merge_requests,
    :foreign_key => :author_id,
    :dependent => :destroy

  has_many :assigned_merge_requests,
    :class_name => "MergeRequest",
    :foreign_key => :assignee_id,
    :dependent => :destroy

V
Valery Sizov 已提交
43 44 45 46
  validates :projects_limit,
            :presence => true,
            :numericality => {:greater_than_or_equal_to => 0}
            
47
  validates :bio, :length => { :within => 0..255 }
V
Valery Sizov 已提交
48

N
Nihad Abbasov 已提交
49
  before_create :ensure_authentication_token
50
  alias_attribute :private_token, :authentication_token
G
gitlabhq 已提交
51 52 53
  scope :not_in_project, lambda { |project|  where("id not in (:ids)", :ids => project.users.map(&:id) ) }

  def identifier
D
Drew 已提交
54
    email.gsub /[@.]/, "_"
G
gitlabhq 已提交
55 56 57 58 59
  end

  def is_admin?
    admin
  end
V
Valera Sizov 已提交
60

61 62 63 64
  def require_ssh_key?
    keys.count == 0
  end

V
Valera Sizov 已提交
65
  def can_create_project?
V
Valery Sizov 已提交
66
    projects_limit > my_own_projects.count
V
Valera Sizov 已提交
67
  end
G
gitlabhq 已提交
68 69 70 71

  def last_activity_project
    projects.first
  end
V
Valery Sizov 已提交
72 73 74

  def self.generate_random_password
    (0...8).map{ ('a'..'z').to_a[rand(26)] }.join
75 76 77 78 79
  end

  def first_name 
    name.split(" ").first unless name.blank?
  end
V
vsizov 已提交
80

81 82
  def self.find_for_ldap_auth(omniauth_info)
    name = omniauth_info.name
83
    email = omniauth_info.email.downcase
V
vsizov 已提交
84 85 86 87 88
    
    if @user = User.find_by_email(email)
      @user
    else
      password = generate_random_password
89
      @user = User.create(:name => name,
V
vsizov 已提交
90 91 92 93 94 95
        :email => email,
        :password => password,
        :password_confirmation => password
      )
    end
  end
96 97 98 99

  def cared_merge_requests
    MergeRequest.where("author_id = :id or assignee_id = :id", :id => self.id).opened
  end
D
Dmitriy Zaporozhets 已提交
100 101 102 103

  def project_ids
    projects.map(&:id)
  end
G
gitlabhq 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
end
# == Schema Information
#
# Table name: users
#
#  id                     :integer         not null, primary key
#  email                  :string(255)     default(""), not null
#  encrypted_password     :string(128)     default(""), not null
#  reset_password_token   :string(255)
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer         default(0)
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
#  created_at             :datetime
#  updated_at             :datetime
#  name                   :string(255)
#  admin                  :boolean         default(FALSE), not null
S
Saito 已提交
124 125 126 127 128
#  projects_limit         :integer         default(10)
#  skype                  :string(255)     default(""), not null
#  linkedin               :string(255)     default(""), not null
#  twitter                :string(255)     default(""), not null
#  authentication_token   :string(255)
V
Valery Sizov 已提交
129
#  dark_scheme            :boolean         default(FALSE), not null
G
gitlabhq 已提交
130 131
#