user.rb 4.2 KB
Newer Older
G
gitlabhq 已提交
1
class User < ActiveRecord::Base
2 3
  include Account

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

N
Nihad Abbasov 已提交
7 8 9
  attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, :name,
                  :skype, :linkedin, :twitter, :dark_scheme, :theme_id, :force_random_password,
                  :extern_uid, :provider, :as => [:default, :admin]
10
  attr_accessible :projects_limit, :as => :admin
11 12

  attr_accessor :force_random_password
G
gitlabhq 已提交
13

N
Nihad Abbasov 已提交
14
  has_many :keys, dependent: :destroy
15
  has_many :projects, through: :users_projects
N
Nihad Abbasov 已提交
16 17 18 19
  has_many :users_projects, dependent: :destroy
  has_many :issues, foreign_key: :author_id, dependent: :destroy
  has_many :notes, foreign_key: :author_id, dependent: :destroy
  has_many :merge_requests, foreign_key: :author_id, dependent: :destroy
20
  has_many :my_own_projects, class_name: "Project", foreign_key: :owner_id
N
Nihad Abbasov 已提交
21 22 23 24
  has_many :events, class_name: "Event", foreign_key: :author_id, dependent: :destroy
  has_many :recent_events, class_name: "Event", foreign_key: :author_id, order: "id DESC"
  has_many :assigned_issues, class_name: "Issue", foreign_key: :assignee_id, dependent: :destroy
  has_many :assigned_merge_requests, class_name: "MergeRequest", foreign_key: :assignee_id, dependent: :destroy
25

26
  validates :bio, length: { within: 0..255 }
27
  validates :extern_uid, :allow_blank => true, :uniqueness => {:scope => :provider}
N
Nihad Abbasov 已提交
28
  validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
29

30 31 32 33
  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)
34

35
  before_validation :generate_password, on: :create
N
Nihad Abbasov 已提交
36 37
  before_save :ensure_authentication_token
  alias_attribute :private_token, :authentication_token
38 39

  def generate_password
J
Jakub Troszok 已提交
40
    if self.force_random_password
41 42 43 44
      self.password = self.password_confirmation = Devise.friendly_token.first(8)
    end
  end

45 46 47 48 49 50 51 52 53 54 55 56 57
  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 已提交
58

F
Florian Unglaub 已提交
59
  def self.create_from_omniauth(auth, ldap = false)
D
Dmitriy Zaporozhets 已提交
60
    gitlab_auth.create_from_omniauth(auth, ldap)
F
Florian Unglaub 已提交
61 62 63
  end

  def self.find_or_new_for_omniauth(auth)
D
Dmitriy Zaporozhets 已提交
64 65
    gitlab_auth.find_or_new_for_omniauth(auth)
  end
66

D
Dmitriy Zaporozhets 已提交
67 68
  def self.find_for_ldap_auth(auth, signed_in_resource = nil)
    gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)
69 70
  end

D
Dmitriy Zaporozhets 已提交
71 72
  def self.gitlab_auth
    Gitlab::Auth.new
V
vsizov 已提交
73
  end
R
randx 已提交
74 75

  def self.search query
V
Valeriy Sizov 已提交
76
    where("name LIKE :query OR email LIKE :query", query: "%#{query}%")
R
randx 已提交
77
  end
G
gitlabhq 已提交
78
end
N
Nihad Abbasov 已提交
79

G
gitlabhq 已提交
80 81 82 83
# == Schema Information
#
# Table name: users
#
N
Nihad Abbasov 已提交
84
#  id                     :integer         not null, primary key
G
gitlabhq 已提交
85 86 87 88 89
#  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
N
Nihad Abbasov 已提交
90
#  sign_in_count          :integer         default(0)
G
gitlabhq 已提交
91 92 93 94
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
R
randx 已提交
95 96
#  created_at             :datetime        not null
#  updated_at             :datetime        not null
G
gitlabhq 已提交
97
#  name                   :string(255)
N
Nihad Abbasov 已提交
98 99
#  admin                  :boolean         default(FALSE), not null
#  projects_limit         :integer         default(10)
S
Saito 已提交
100 101 102 103
#  skype                  :string(255)     default(""), not null
#  linkedin               :string(255)     default(""), not null
#  twitter                :string(255)     default(""), not null
#  authentication_token   :string(255)
N
Nihad Abbasov 已提交
104 105
#  dark_scheme            :boolean         default(FALSE), not null
#  theme_id               :integer         default(1), not null
R
randx 已提交
106
#  bio                    :string(255)
N
Nihad Abbasov 已提交
107 108 109 110 111
#  blocked                :boolean         default(FALSE), not null
#  failed_attempts        :integer         default(0)
#  locked_at              :datetime
#  extern_uid             :string(255)
#  provider               :string(255)
G
gitlabhq 已提交
112
#