Read about our upcoming Code of Conduct on this issue

user.rb 4.29 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class User < ActiveRecord::Base
2
3
  include Account

4
  devise :database_authenticatable, :token_authenticatable, :lockable,
Valery Sizov's avatar
Valery Sizov committed
5
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable
gitlabhq's avatar
gitlabhq committed
6

Nihad Abbasov's avatar
Nihad Abbasov committed
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
gitlabhq's avatar
gitlabhq committed
13

Nihad Abbasov's avatar
Nihad Abbasov committed
14
  has_many :keys, dependent: :destroy
15
  has_many :projects, through: :users_projects
Nihad Abbasov's avatar
Nihad Abbasov committed
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
Nihad Abbasov's avatar
Nihad Abbasov committed
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}
Nihad Abbasov's avatar
Nihad Abbasov committed
28
  validates :projects_limit, presence: true, numericality: {greater_than_or_equal_to: 0}
29

30
  before_validation :generate_password, on: :create
Nihad Abbasov's avatar
Nihad Abbasov committed
31
32
  before_save :ensure_authentication_token
  alias_attribute :private_token, :authentication_token
33

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  # Scopes
  scope :not_in_project, ->(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)

  class << self
    def filter filter_name
      case filter_name
      when "admins"; self.admins
      when "blocked"; self.blocked
      when "wop"; self.without_projects
      else
        self.active
      end
49
50
    end

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
51
52
    def without_projects
      where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
53
54
    end

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
55
56
57
    def create_from_omniauth(auth, ldap = false)
      gitlab_auth.create_from_omniauth(auth, ldap)
    end
gitlabhq's avatar
gitlabhq committed
58

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
59
60
61
    def find_or_new_for_omniauth(auth)
      gitlab_auth.find_or_new_for_omniauth(auth)
    end
Florian Unglaub's avatar
Florian Unglaub committed
62

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
63
64
65
    def find_for_ldap_auth(auth, signed_in_resource = nil)
      gitlab_auth.find_for_ldap_auth(auth, signed_in_resource)
    end
66

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
67
68
69
    def gitlab_auth
      Gitlab::Auth.new
    end
70

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
71
72
73
    def search query
      where("name LIKE :query or email LIKE :query", query: "%#{query}%")
    end
vsizov's avatar
vsizov committed
74
  end
randx's avatar
randx committed
75

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
76
77
78
79
  def generate_password
    if self.force_random_password
      self.password = self.password_confirmation = Devise.friendly_token.first(8)
    end
randx's avatar
randx committed
80
  end
gitlabhq's avatar
gitlabhq committed
81
end
Nihad Abbasov's avatar
Nihad Abbasov committed
82

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