Read about our upcoming Code of Conduct on this issue

users_project.rb 2.57 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class UsersProject < ActiveRecord::Base
2
3
  include GitHost

4
5
6
7
  GUEST     = 10
  REPORTER  = 20
  DEVELOPER = 30
  MASTER    = 40
8

9
10
  attr_accessible :user, :user_id, :project_access

gitlabhq's avatar
gitlabhq committed
11
12
13
  belongs_to :user
  belongs_to :project

14
15
  after_save :update_repository
  after_destroy :update_repository
gitlabhq's avatar
gitlabhq committed
16

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
17
18
19
  validates :user, presence: true
  validates :user_id, uniqueness: { :scope => [:project_id], message: "already exists in project" }
  validates :project, presence: true
20

21
  delegate :name, :email, to: :user, prefix: true
gitlabhq's avatar
gitlabhq committed
22

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
23
24
25
26
27
28
  class << self
    def bulk_delete(project, user_ids)
      UsersProject.transaction do
        UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
          users_project.destroy
        end
miks's avatar
miks committed
29
30
31
      end
    end

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
32
33
34
35
36
37
    def bulk_update(project, user_ids, project_access)
      UsersProject.transaction do
        UsersProject.where(:user_id => user_ids, :project_id => project.id).each do |users_project|
          users_project.project_access = project_access
          users_project.save
        end
miks's avatar
miks committed
38
39
40
      end
    end

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
41
42
43
44
45
46
47
48
49
50
    def bulk_import(project, user_ids, project_access)
      UsersProject.transaction do
        user_ids.each do |user_id|
          users_project = UsersProject.new(
            project_access: project_access,
            user_id: user_id
          )
          users_project.project = project
          users_project.save
        end
51
52
53
      end
    end

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
54
55
56
57
58
59
60
61
62
63
    def user_bulk_import(user, project_ids, project_access)
      UsersProject.transaction do
        project_ids.each do |project_id|
          users_project = UsersProject.new(
            project_access: project_access,
          )
          users_project.project_id = project_id
          users_project.user_id = user.id
          users_project.save
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
64
65
66
      end
    end

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
67
68
69
70
71
72
73
74
    def access_roles
      {
        "Guest"     => GUEST,
        "Reporter"  => REPORTER,
        "Developer" => DEVELOPER,
        "Master"    => MASTER
      }
    end
75
76
77
  end

  def role_access
78
    project_access
79
80
  end

81
  def update_repository
82
    git_host.update_repository(project)
gitlabhq's avatar
gitlabhq committed
83
  end
84
85
86
87
88
89

  def project_access_human
    Project.access_options.key(self.project_access)
  end

  def repo_access_human
Valeriy Sizov's avatar
Valeriy Sizov committed
90
    self.class.access_roles.invert[self.project_access]
91
  end
gitlabhq's avatar
gitlabhq committed
92
end
Nihad Abbasov's avatar
Nihad Abbasov committed
93

gitlabhq's avatar
gitlabhq committed
94
95
96
97
# == Schema Information
#
# Table name: users_projects
#
Nihad Abbasov's avatar
Nihad Abbasov committed
98
99
100
#  id             :integer         not null, primary key
#  user_id        :integer         not null
#  project_id     :integer         not null
randx's avatar
randx committed
101
102
#  created_at     :datetime        not null
#  updated_at     :datetime        not null
Nihad Abbasov's avatar
Nihad Abbasov committed
103
#  project_access :integer         default(0), not null
gitlabhq's avatar
gitlabhq committed
104
#