user.rb 2.9 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,
G
gitlabhq 已提交
5 6 7
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
N
Nihad Abbasov 已提交
8
  attr_accessible :email, :password, :password_confirmation, :remember_me,
9
                  :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme
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 15 16 17 18
  has_many :keys, :dependent => :destroy
  has_many :issues,
    :foreign_key => :author_id,
    :dependent => :destroy

19 20 21 22
  has_many :notes,
    :foreign_key => :author_id,
    :dependent => :destroy

G
gitlabhq 已提交
23 24 25 26 27
  has_many :assigned_issues,
    :class_name => "Issue",
    :foreign_key => :assignee_id,
    :dependent => :destroy

28 29 30 31 32 33 34 35 36
  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 已提交
37 38 39 40 41
  validates :projects_limit,
            :presence => true,
            :numericality => {:greater_than_or_equal_to => 0}
            

N
Nihad Abbasov 已提交
42
  before_create :ensure_authentication_token
43
  alias_attribute :private_token, :authentication_token
G
gitlabhq 已提交
44 45 46
  scope :not_in_project, lambda { |project|  where("id not in (:ids)", :ids => project.users.map(&:id) ) }

  def identifier
D
Drew 已提交
47
    email.gsub /[@.]/, "_"
G
gitlabhq 已提交
48 49 50 51 52
  end

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

54 55 56 57
  def require_ssh_key?
    keys.count == 0
  end

V
Valera Sizov 已提交
58
  def can_create_project?
V
Valery Sizov 已提交
59
    projects_limit > my_own_projects.count
V
Valera Sizov 已提交
60
  end
G
gitlabhq 已提交
61 62 63 64

  def last_activity_project
    projects.first
  end
G
gitlabhq 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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 已提交
85 86 87 88 89
#  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 已提交
90
#  dark_scheme            :boolean         default(FALSE), not null
G
gitlabhq 已提交
91 92
#