user.rb 5.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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  def self.find_or_new_for_omniauth(oauth)
    provider, uid = oauth['provider'], oauth['uid']

    if @user = User.find_by_provider_and_extern_uid(provider, uid)
      @user
    else
      if Gitlab.config.omniauth.allow_single_sign_on
        # Ensure here that all required attributes were passed along with the
        # oauth request:
        %w(first_name last_name email).each do |attr|
          unless oauth[:info][attr].present?
            raise OmniAuth::Error,
                  "#{provider} does not provide the required field #{attr}"
          end
        end

        password = Devise.friendly_token[0, 8].downcase
        @user = User.new(
          extern_uid: uid,
          provider: provider,
          name: "#{oauth[:info][:first_name]} #{oauth[:info][:last_name]}",
          email: oauth[:info][:email],
          password: password,
          password_confirmation: password,
          projects_limit: Gitlab.config.default_projects_limit,
        )

        @user.blocked = true if Gitlab.config.omniauth.block_auto_created_users
117 118
        @user.save!

119 120 121 122 123
        @user
      end
    end
  end

124 125 126 127 128
  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?
129
    raise OmniAuth::Error, "LDAP accounts must provide an uid and email address" if uid.nil? or email.nil?
130

131
    if @user = User.find_by_extern_uid_and_provider(uid, provider)
V
vsizov 已提交
132
      @user
133 134 135 136
    # 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 已提交
137 138
      @user
    else
139
      logger.info "Creating user from LDAP login {uid => #{uid}, name => #{name}, email => #{email}}"
N
Nihad Abbasov 已提交
140 141
      password = Devise.friendly_token[0, 8].downcase
      @user = User.create(
142 143
        :extern_uid => uid,
        :provider => provider,
N
Nihad Abbasov 已提交
144
        :name => name,
V
vsizov 已提交
145 146
        :email => email,
        :password => password,
147
        :password_confirmation => password,
148
        :projects_limit => Gitlab.config.default_projects_limit
V
vsizov 已提交
149 150 151
      )
    end
  end
R
randx 已提交
152 153

  def self.search query
154
    where("name like :query or email like :query", query: "%#{query}%")
R
randx 已提交
155
  end
G
gitlabhq 已提交
156 157 158 159 160
end
# == Schema Information
#
# Table name: users
#
R
randx 已提交
161
#  id                     :integer(4)      not null, primary key
G
gitlabhq 已提交
162 163 164 165 166
#  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 已提交
167
#  sign_in_count          :integer(4)      default(0)
G
gitlabhq 已提交
168 169 170 171
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
R
randx 已提交
172 173
#  created_at             :datetime        not null
#  updated_at             :datetime        not null
G
gitlabhq 已提交
174
#  name                   :string(255)
R
randx 已提交
175 176
#  admin                  :boolean(1)      default(FALSE), not null
#  projects_limit         :integer(4)      default(10)
S
Saito 已提交
177 178 179 180
#  skype                  :string(255)     default(""), not null
#  linkedin               :string(255)     default(""), not null
#  twitter                :string(255)     default(""), not null
#  authentication_token   :string(255)
R
randx 已提交
181 182 183 184
#  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 已提交
185
#