user.rb 4.9 KB
Newer Older
G
gitlabhq 已提交
1
class User < ActiveRecord::Base
2

3 4
  include Account

5
  devise :database_authenticatable, :token_authenticatable, :lockable,
V
Valery Sizov 已提交
6
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable
G
gitlabhq 已提交
7

8
  attr_accessible :email, :password, :password_confirmation, :remember_me, :bio,
9
                  :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme,
10
                  :theme_id, :force_random_password, :extern_uid, :provider
11 12

  attr_accessor :force_random_password
G
gitlabhq 已提交
13

14 15 16 17
  has_many :users_projects, dependent: :destroy
  has_many :projects, through: :users_projects
  has_many :my_own_projects, class_name: "Project", foreign_key: :owner_id
  has_many :keys, dependent: :destroy
18

19
  has_many :events,
20 21 22
    class_name: "Event",
    foreign_key: :author_id,
    dependent: :destroy
23

24
  has_many :recent_events,
25 26 27
    class_name: "Event",
    foreign_key: :author_id,
    order: "id DESC"
28

G
gitlabhq 已提交
29
  has_many :issues,
30 31
    foreign_key: :author_id,
    dependent: :destroy
G
gitlabhq 已提交
32

33
  has_many :notes,
34 35
    foreign_key: :author_id,
    dependent: :destroy
36

G
gitlabhq 已提交
37
  has_many :assigned_issues,
38 39 40
    class_name: "Issue",
    foreign_key: :assignee_id,
    dependent: :destroy
G
gitlabhq 已提交
41

42
  has_many :merge_requests,
43 44
    foreign_key: :author_id,
    dependent: :destroy
45 46

  has_many :assigned_merge_requests,
47 48 49
    class_name: "MergeRequest",
    foreign_key: :assignee_id,
    dependent: :destroy
50

V
Valery Sizov 已提交
51
  validates :projects_limit,
52 53
            presence: true,
            numericality: {greater_than_or_equal_to: 0}
54

55
  validates :bio, length: { within: 0..255 }
V
Valery Sizov 已提交
56

57 58
  validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}

N
Nihad Abbasov 已提交
59
  before_save :ensure_authentication_token
60
  alias_attribute :private_token, :authentication_token
61

62 63 64 65
  scope :not_in_project, lambda { |project|  where("id not in (:ids)", ids: project.users.map(&:id) ) }
  scope :admins, where(admin:  true)
  scope :blocked, where(blocked:  true)
  scope :active, where(blocked:  false)
66

67
  before_validation :generate_password, on: :create
68 69

  def generate_password
J
Jakub Troszok 已提交
70
    if self.force_random_password
71 72 73 74
      self.password = self.password_confirmation = Devise.friendly_token.first(8)
    end
  end

75 76 77 78 79 80 81 82 83 84 85 86 87
  def self.filter filter_name
    case filter_name
    when "admins"; self.admins
    when "blocked"; self.blocked
    when "wop"; self.without_projects
    else
      self.active
    end
  end

  def self.without_projects
    where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
  end
G
gitlabhq 已提交
88

89 90 91 92 93
  def self.find_for_ldap_auth(auth, signed_in_resource=nil)
    uid = auth.info.uid
    provider = auth.provider
    name = auth.info.name.force_encoding("utf-8")
    email = auth.info.email.downcase unless auth.info.email.nil?
94
    raise OmniAuth::Error, "LDAP accounts must provide an uid and email address" if uid.nil? or email.nil?
95

96
    if @user = User.find_by_extern_uid_and_provider(uid, provider)
V
vsizov 已提交
97
      @user
98 99 100 101
    # workaround for backward compatibility
    elsif @user = User.find_by_email(email)
      logger.info "Updating legacy LDAP user #{email} with extern_uid => #{uid}"
      @user.update_attributes(:extern_uid => uid, :provider => provider)
V
vsizov 已提交
102 103
      @user
    else
104
      logger.info "Creating user from LDAP login {uid => #{uid}, name => #{name}, email => #{email}}"
N
Nihad Abbasov 已提交
105 106
      password = Devise.friendly_token[0, 8].downcase
      @user = User.create(
107 108
        :extern_uid => uid,
        :provider => provider,
N
Nihad Abbasov 已提交
109
        :name => name,
V
vsizov 已提交
110 111
        :email => email,
        :password => password,
112
        :password_confirmation => password,
113
        :projects_limit => Gitlab.config.default_projects_limit
V
vsizov 已提交
114 115 116
      )
    end
  end
R
randx 已提交
117 118

  def self.search query
119
    where("name like :query or email like :query", query: "%#{query}%")
R
randx 已提交
120
  end
G
gitlabhq 已提交
121 122 123 124 125
end
# == Schema Information
#
# Table name: users
#
R
randx 已提交
126
#  id                     :integer(4)      not null, primary key
G
gitlabhq 已提交
127 128 129 130 131
#  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
R
randx 已提交
132
#  sign_in_count          :integer(4)      default(0)
G
gitlabhq 已提交
133 134 135 136
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
R
randx 已提交
137 138
#  created_at             :datetime        not null
#  updated_at             :datetime        not null
G
gitlabhq 已提交
139
#  name                   :string(255)
R
randx 已提交
140 141
#  admin                  :boolean(1)      default(FALSE), not null
#  projects_limit         :integer(4)      default(10)
S
Saito 已提交
142 143 144 145
#  skype                  :string(255)     default(""), not null
#  linkedin               :string(255)     default(""), not null
#  twitter                :string(255)     default(""), not null
#  authentication_token   :string(255)
R
randx 已提交
146 147 148 149
#  dark_scheme            :boolean(1)      default(FALSE), not null
#  theme_id               :integer(4)      default(1), not null
#  bio                    :string(255)
#  blocked                :boolean(1)      default(FALSE), not null
G
gitlabhq 已提交
150
#
R
randx 已提交
151