Commit 1c62ec09 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

4 roles permission system

parent dac7c44a
...@@ -28,7 +28,7 @@ class ProjectsController < ApplicationController ...@@ -28,7 +28,7 @@ class ProjectsController < ApplicationController
Project.transaction do Project.transaction do
@project.save! @project.save!
@project.users_projects.create!(:repo_access => Repository::REPO_RW , :project_access => Project::PROJECT_RWA, :user => current_user) @project.users_projects.create!(:project_access => UsersProject::MASTER, :user => current_user)
# when project saved no team member exist so # when project saved no team member exist so
# project repository should be updated after first user add # project repository should be updated after first user add
......
require "grit" require "grit"
class Project < ActiveRecord::Base class Project < ActiveRecord::Base
PROJECT_N = 0
PROJECT_R = 1
PROJECT_RW = 2
PROJECT_RWA = 3
belongs_to :owner, :class_name => "User" belongs_to :owner, :class_name => "User"
has_many :merge_requests, :dependent => :destroy has_many :merge_requests, :dependent => :destroy
...@@ -61,12 +56,7 @@ class Project < ActiveRecord::Base ...@@ -61,12 +56,7 @@ class Project < ActiveRecord::Base
end end
def self.access_options def self.access_options
{ UsersProject.access_roles
"Denied" => PROJECT_N,
"Read" => PROJECT_R,
"Report" => PROJECT_RW,
"Admin" => PROJECT_RWA
}
end end
def repository def repository
...@@ -193,11 +183,11 @@ class Project < ActiveRecord::Base ...@@ -193,11 +183,11 @@ class Project < ActiveRecord::Base
# Should be rewrited for new access rights # Should be rewrited for new access rights
def add_access(user, *access) def add_access(user, *access)
access = if access.include?(:admin) access = if access.include?(:admin)
{ :project_access => PROJECT_RWA } { :project_access => UsersProject::MASTER }
elsif access.include?(:write) elsif access.include?(:write)
{ :project_access => PROJECT_RW } { :project_access => UsersProject::DEVELOPER }
else else
{ :project_access => PROJECT_R } { :project_access => UsersProject::GUEST }
end end
opts = { :user => user } opts = { :user => user }
opts.merge!(access) opts.merge!(access)
...@@ -210,48 +200,48 @@ class Project < ActiveRecord::Base ...@@ -210,48 +200,48 @@ class Project < ActiveRecord::Base
def repository_readers def repository_readers
keys = Key.joins({:user => :users_projects}). keys = Key.joins({:user => :users_projects}).
where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R) where("users_projects.project_id = ? AND users_projects.project_access = ?", id, UsersProject::REPORTER)
keys.map(&:identifier) + deploy_keys.map(&:identifier) keys.map(&:identifier) + deploy_keys.map(&:identifier)
end end
def repository_writers def repository_writers
keys = Key.joins({:user => :users_projects}). keys = Key.joins({:user => :users_projects}).
where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_RW) where("users_projects.project_id = ? AND users_projects.project_access = ?", id, UsersProject::DEVELOPER)
keys.map(&:identifier) keys.map(&:identifier)
end end
def repository_masters def repository_masters
keys = Key.joins({:user => :users_projects}). keys = Key.joins({:user => :users_projects}).
where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_MASTER) where("users_projects.project_id = ? AND users_projects.project_access = ?", id, UsersProject::MASTER)
keys.map(&:identifier) keys.map(&:identifier)
end end
def readers def readers
@readers ||= users_projects.includes(:user).where(:project_access => [PROJECT_R, PROJECT_RW, PROJECT_RWA]).map(&:user) @readers ||= users_projects.includes(:user).map(&:user)
end end
def writers def writers
@writers ||= users_projects.includes(:user).where(:project_access => [PROJECT_RW, PROJECT_RWA]).map(&:user) @writers ||= users_projects.includes(:user).map(&:user)
end end
def admins def admins
@admins ||= users_projects.includes(:user).where(:project_access => PROJECT_RWA).map(&:user) @admins ||= users_projects.includes(:user).where(:project_access => UsersProject::MASTER).map(&:user)
end end
def allow_read_for?(user) def allow_read_for?(user)
!users_projects.where(:user_id => user.id, :project_access => [PROJECT_R, PROJECT_RW, PROJECT_RWA]).empty? !users_projects.where(:user_id => user.id).empty?
end end
def allow_write_for?(user) def allow_write_for?(user)
!users_projects.where(:user_id => user.id, :project_access => [PROJECT_RW, PROJECT_RWA]).empty? !users_projects.where(:user_id => user.id).empty?
end end
def allow_admin_for?(user) def allow_admin_for?(user)
!users_projects.where(:user_id => user.id, :project_access => [PROJECT_RWA]).empty? || owner_id == user.id !users_projects.where(:user_id => user.id, :project_access => [UsersProject::MASTER]).empty? || owner_id == user.id
end end
def allow_pull_for?(user) def allow_pull_for?(user)
!users_projects.where(:user_id => user.id, :repo_access => [Repository::REPO_R, Repository::REPO_RW, Repository::REPO_MASTER]).empty? !users_projects.where(:user_id => user.id, :project_access => [UsersProject::REPORTER, UsersProject::DEVELOPER, UsersProject::MASTER]).empty?
end end
def root_ref def root_ref
......
require File.join(Rails.root, "lib", "gitlabhq", "git_host") require File.join(Rails.root, "lib", "gitlabhq", "git_host")
class Repository class Repository
REPO_N = 0
REPO_R = 1
REPO_RW = 2
REPO_MASTER = 3
attr_accessor :project attr_accessor :project
def self.default_ref def self.default_ref
...@@ -13,12 +8,7 @@ class Repository ...@@ -13,12 +8,7 @@ class Repository
end end
def self.access_options def self.access_options
{ {}
"Denied" => REPO_N,
"Pull" => REPO_R,
"Pull & Push" => REPO_RW,
"Master" => REPO_MASTER
}
end end
def initialize(project) def initialize(project)
......
class UsersProject < ActiveRecord::Base class UsersProject < ActiveRecord::Base
REPORTER = 21 GUEST = 10
DEVELOPER = 22 REPORTER = 20
MASTER = 33 DEVELOPER = 30
MASTER = 40
belongs_to :user belongs_to :user
belongs_to :project belongs_to :project
...@@ -21,7 +22,6 @@ class UsersProject < ActiveRecord::Base ...@@ -21,7 +22,6 @@ class UsersProject < ActiveRecord::Base
UsersProject.transaction do UsersProject.transaction do
user_ids.each do |user_id| user_ids.each do |user_id|
users_project = UsersProject.new( users_project = UsersProject.new(
:repo_access => repo_access,
:project_access => project_access, :project_access => project_access,
:user_id => user_id :user_id => user_id
) )
...@@ -35,7 +35,6 @@ class UsersProject < ActiveRecord::Base ...@@ -35,7 +35,6 @@ class UsersProject < ActiveRecord::Base
UsersProject.transaction do UsersProject.transaction do
project_ids.each do |project_id| project_ids.each do |project_id|
users_project = UsersProject.new( users_project = UsersProject.new(
:repo_access => repo_access,
:project_access => project_access, :project_access => project_access,
) )
users_project.project_id = project_id users_project.project_id = project_id
...@@ -47,6 +46,7 @@ class UsersProject < ActiveRecord::Base ...@@ -47,6 +46,7 @@ class UsersProject < ActiveRecord::Base
def self.access_roles def self.access_roles
{ {
"Guest" => GUEST,
"Reporter" => REPORTER, "Reporter" => REPORTER,
"Developer" => DEVELOPER, "Developer" => DEVELOPER,
"Master" => MASTER "Master" => MASTER
...@@ -54,7 +54,7 @@ class UsersProject < ActiveRecord::Base ...@@ -54,7 +54,7 @@ class UsersProject < ActiveRecord::Base
end end
def role_access def role_access
"#{project_access}#{repo_access}" project_access
end end
def update_repository def update_repository
...@@ -68,7 +68,7 @@ class UsersProject < ActiveRecord::Base ...@@ -68,7 +68,7 @@ class UsersProject < ActiveRecord::Base
end end
def repo_access_human def repo_access_human
Repository.access_options.key(self.repo_access) ""
end end
end end
# == Schema Information # == Schema Information
......
...@@ -53,7 +53,6 @@ ...@@ -53,7 +53,6 @@
%td %td
= link_to tm.user_name, admin_users_path(tm.user) = link_to tm.user_name, admin_users_path(tm.user)
%td= select_tag :tm_project_access, options_for_select(Project.access_options, tm.project_access), :class => "medium project-access-select", :disabled => :disabled %td= select_tag :tm_project_access, options_for_select(Project.access_options, tm.project_access), :class => "medium project-access-select", :disabled => :disabled
%td= select_tag :tm_repo_access, options_for_select(Repository.access_options, tm.repo_access), :class => "medium repo-access-select", :disabled => :disabled
%td= link_to 'Edit Access', edit_admin_team_member_path(tm), :class => "btn small" %td= link_to 'Edit Access', edit_admin_team_member_path(tm), :class => "btn small"
%td= link_to 'Remove from team', admin_team_member_path(tm), :confirm => 'Are you sure?', :method => :delete, :class => "btn danger small" %td= link_to 'Remove from team', admin_team_member_path(tm), :confirm => 'Are you sure?', :method => :delete, :class => "btn danger small"
...@@ -68,7 +67,6 @@ ...@@ -68,7 +67,6 @@
%tr %tr
%td= select_tag :user_ids, options_from_collection_for_select(@users , :id, :name), :multiple => true %td= select_tag :user_ids, options_from_collection_for_select(@users , :id, :name), :multiple => true
%td= select_tag :project_access, options_for_select(Project.access_options), :class => "project-access-select" %td= select_tag :project_access, options_for_select(Project.access_options), :class => "project-access-select"
%td= select_tag :repo_access, options_for_select(Repository.access_options), :class => "repo-access-select"
.actions .actions
= submit_tag 'Add', :class => "btn primary" = submit_tag 'Add', :class => "btn primary"
......
...@@ -10,10 +10,6 @@ ...@@ -10,10 +10,6 @@
.input .input
= f.select :project_access, options_for_select(Project.access_options, @admin_team_member.project_access), {}, :class => "project-access-select" = f.select :project_access, options_for_select(Project.access_options, @admin_team_member.project_access), {}, :class => "project-access-select"
.clearfix
%label Repository Access:
.input
= f.select :repo_access, options_for_select(Repository.access_options, @admin_team_member.repo_access), {}, :class => "repo-access-select"
%br %br
.actions .actions
= f.submit 'Save', :class => "btn primary" = f.submit 'Save', :class => "btn primary"
......
...@@ -61,7 +61,6 @@ ...@@ -61,7 +61,6 @@
%tr %tr
%td= link_to project.name, admin_project_path(project) %td= link_to project.name, admin_project_path(project)
%td= select_tag :tm_project_access, options_for_select(Project.access_options, tm.project_access), :class => "medium project-access-select", :disabled => :disabled %td= select_tag :tm_project_access, options_for_select(Project.access_options, tm.project_access), :class => "medium project-access-select", :disabled => :disabled
%td= select_tag :tm_repo_access, options_for_select(Repository.access_options, tm.repo_access), :class => "medium repo-access-select", :disabled => :disabled
%td= link_to 'Edit Access', edit_admin_team_member_path(tm), :class => "btn small" %td= link_to 'Edit Access', edit_admin_team_member_path(tm), :class => "btn small"
%td= link_to 'Remove from team', admin_team_member_path(tm), :confirm => 'Are you sure?', :method => :delete, :class => "btn small danger" %td= link_to 'Remove from team', admin_team_member_path(tm), :confirm => 'Are you sure?', :method => :delete, :class => "btn small danger"
...@@ -76,7 +75,6 @@ ...@@ -76,7 +75,6 @@
%tr %tr
%td= select_tag :project_ids, options_from_collection_for_select(@projects , :id, :name), :multiple => true %td= select_tag :project_ids, options_from_collection_for_select(@projects , :id, :name), :multiple => true
%td= select_tag :project_access, options_for_select(Project.access_options), :class => "project-access-select" %td= select_tag :project_access, options_for_select(Project.access_options), :class => "project-access-select"
%td= select_tag :repo_access, options_for_select(Repository.access_options), :class => "repo-access-select"
.actions .actions
= submit_tag 'Add', :class => "btn primary" = submit_tag 'Add', :class => "btn primary"
......
%h3 Permissions %h3 Permissions
%hr %hr
%h4 Reporter
%ul
%li Create new issue
%li Create new merge request
%li Write on project wall
%h4 Reporter %h4 Reporter
%ul %ul
%li Pull project code %li Pull project code
......
...@@ -14,18 +14,9 @@ ...@@ -14,18 +14,9 @@
.clearfix .clearfix
= f.label :project_access, "Project Access" = f.label :project_access, "Project Access"
.input= f.select :_project_access, options_for_select(UsersProject.access_roles, @team_member.role_access), {}, :class => "project-access-select" .input= f.select :project_access, options_for_select(Project.access_options, @team_member.project_access), {}, :class => "project-access-select"
-#.clearfix
-#= f.label :project_access, "Project Access"
-#.input= f.select :project_access, options_for_select(Project.access_options, @team_member.project_access), {}, :class => "project-access-select"
-#.clearfix
-#= f.label :repo_access, "Repository Access"
-#.input= f.select :repo_access, options_for_select(Repository.access_options, @team_member.repo_access), {}, :class => "repo-access-select"
.actions .actions
= f.submit 'Save', :class => "btn primary" = f.submit 'Save', :class => "btn primary"
= link_to "Cancel", team_project_path(@project), :class => "btn" = link_to "Cancel", team_project_path(@project), :class => "btn"
...@@ -37,6 +28,6 @@ ...@@ -37,6 +28,6 @@
:javascript :javascript
$('select#team_member_user_id').chosen(); $('select#team_member_user_id').chosen();
$('select#team_member__project_access').chosen(); $('select#team_member_project_access').chosen();
//$('select#team_member_repo_access').chosen(); //$('select#team_member_repo_access').chosen();
//$('select#team_member_project_access').chosen(); //$('select#team_member_project_access').chosen();
...@@ -11,9 +11,6 @@ ...@@ -11,9 +11,6 @@
.span3 .span3
= form_for(member, :as => :team_member, :url => project_team_member_path(@project, member)) do |f| = form_for(member, :as => :team_member, :url => project_team_member_path(@project, member)) do |f|
= f.select :_project_access, options_for_select(UsersProject.access_roles, member.role_access), {}, :class => "medium project-access-select", :disabled => !allow_admin = f.select :project_access, options_for_select(UsersProject.access_roles, member.project_access), {}, :class => "medium project-access-select", :disabled => !allow_admin
-#.span3
-#= form_for(member, :as => :team_member, :url => project_team_member_path(@project, member)) do |f|
-#= f.select :repo_access, options_for_select(Repository.access_options, member.repo_access), {}, :class => "medium repo-access-select", :disabled => !allow_admin
- if @project.owner == user - if @project.owner == user
%span.label Project Owner %span.label Project Owner
...@@ -28,13 +28,6 @@ ...@@ -28,13 +28,6 @@
= form_for(@team_member, :as => :team_member, :url => project_team_member_path(@project, @team_member)) do |f| = form_for(@team_member, :as => :team_member, :url => project_team_member_path(@project, @team_member)) do |f|
= f.select :project_access, options_for_select(Project.access_options, @team_member.project_access), {}, :class => "project-access-select", :disabled => !allow_admin = f.select :project_access, options_for_select(Project.access_options, @team_member.project_access), {}, :class => "project-access-select", :disabled => !allow_admin
%tr
%td Repository Access
%td
= form_for(@team_member, :as => :team_member, :url => project_team_member_path(@project, @team_member)) do |f|
= f.select :repo_access, options_for_select(Repository.access_options, @team_member.repo_access), {}, :class => "repo-access-select", :disabled => !allow_admin
- unless user.skype.empty? - unless user.skype.empty?
%tr %tr
%td Skype: %td Skype:
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
.span3 .span3
%span.label= member.project_access_human %span.label= member.project_access_human
%span.label= member.repo_access_human
- if can? current_user, :write_project, @project - if can? current_user, :write_project, @project
- if @project.issues_enabled && @project.merge_requests_enabled - if @project.issues_enabled && @project.merge_requests_enabled
......
class MoveToRolesPermissions < ActiveRecord::Migration
def up
repo_n = 0
repo_r = 1
repo_rw = 2
project_rwa = 3
UsersProject.update_all ["project_access = ?", UsersProject::MASTER], ["project_access = ?", project_rwa]
UsersProject.update_all ["project_access = ?", UsersProject::DEVELOPER], ["repo_access = ?", repo_rw]
UsersProject.update_all ["project_access = ?", UsersProject::REPORTER], ["repo_access = ?", repo_r]
UsersProject.update_all ["project_access = ?", UsersProject::GUEST], ["repo_access = ?", repo_n]
remove_column :users_projects, :repo_access
end
def down
end
end
...@@ -11,19 +11,7 @@ ...@@ -11,19 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120215182305) do ActiveRecord::Schema.define(:version => 20120216085842) do
create_table "features", :force => true do |t|
t.string "name"
t.string "branch_name"
t.integer "assignee_id"
t.integer "author_id"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "version"
t.integer "status", :default => 0, :null => false
end
create_table "issues", :force => true do |t| create_table "issues", :force => true do |t|
t.string "title" t.string "title"
...@@ -160,7 +148,6 @@ ActiveRecord::Schema.define(:version => 20120215182305) do ...@@ -160,7 +148,6 @@ ActiveRecord::Schema.define(:version => 20120215182305) do
t.integer "project_id", :null => false t.integer "project_id", :null => false
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "repo_access", :default => 0, :null => false
t.integer "project_access", :default => 0, :null => false t.integer "project_access", :default => 0, :null => false
end end
......
...@@ -64,9 +64,8 @@ describe Note do ...@@ -64,9 +64,8 @@ describe Note do
describe :read do describe :read do
before do before do
@p1.users_projects.create(:user => @u1, :project_access => Project::PROJECT_N) @p1.users_projects.create(:user => @u2, :project_access => UsersProject::GUEST)
@p1.users_projects.create(:user => @u2, :project_access => Project::PROJECT_R) @p2.users_projects.create(:user => @u3, :project_access => UsersProject::GUEST)
@p2.users_projects.create(:user => @u3, :project_access => Project::PROJECT_R)
end end
it { @abilities.allowed?(@u1, :read_note, @p1).should be_false } it { @abilities.allowed?(@u1, :read_note, @p1).should be_false }
...@@ -76,9 +75,8 @@ describe Note do ...@@ -76,9 +75,8 @@ describe Note do
describe :write do describe :write do
before do before do
@p1.users_projects.create(:user => @u1, :project_access => Project::PROJECT_R) @p1.users_projects.create(:user => @u2, :project_access => UsersProject::DEVELOPER)
@p1.users_projects.create(:user => @u2, :project_access => Project::PROJECT_RW) @p2.users_projects.create(:user => @u3, :project_access => UsersProject::DEVELOPER)
@p2.users_projects.create(:user => @u3, :project_access => Project::PROJECT_RW)
end end
it { @abilities.allowed?(@u1, :write_note, @p1).should be_false } it { @abilities.allowed?(@u1, :write_note, @p1).should be_false }
...@@ -88,9 +86,9 @@ describe Note do ...@@ -88,9 +86,9 @@ describe Note do
describe :admin do describe :admin do
before do before do
@p1.users_projects.create(:user => @u1, :project_access => Project::PROJECT_R) @p1.users_projects.create(:user => @u1, :project_access => UsersProject::REPORTER)
@p1.users_projects.create(:user => @u2, :project_access => Project::PROJECT_RWA) @p1.users_projects.create(:user => @u2, :project_access => UsersProject::MASTER)
@p2.users_projects.create(:user => @u3, :project_access => Project::PROJECT_RWA) @p2.users_projects.create(:user => @u3, :project_access => UsersProject::MASTER)
end end
it { @abilities.allowed?(@u1, :admin_note, @p1).should be_false } it { @abilities.allowed?(@u1, :admin_note, @p1).should be_false }
......
...@@ -12,8 +12,7 @@ describe Project do ...@@ -12,8 +12,7 @@ describe Project do
describe "read access" do describe "read access" do
before do before do
@p1.users_projects.create(:project => @p1, :user => @u1, :project_access => Project::PROJECT_N) @p1.users_projects.create(:project => @p1, :user => @u2, :project_access => UsersProject::REPORTER)
@p1.users_projects.create(:project => @p1, :user => @u2, :project_access => Project::PROJECT_R)
end end
it { @abilities.allowed?(@u1, :read_project, @p1).should be_false } it { @abilities.allowed?(@u1, :read_project, @p1).should be_false }
...@@ -22,8 +21,7 @@ describe Project do ...@@ -22,8 +21,7 @@ describe Project do
describe "write access" do describe "write access" do
before do before do
@p1.users_projects.create(:project => @p1, :user => @u1, :project_access => Project::PROJECT_R) @p1.users_projects.create(:project => @p1, :user => @u2, :project_access => UsersProject::DEVELOPER)
@p1.users_projects.create(:project => @p1, :user => @u2, :project_access => Project::PROJECT_RW)
end end
it { @abilities.allowed?(@u1, :write_project, @p1).should be_false } it { @abilities.allowed?(@u1, :write_project, @p1).should be_false }
...@@ -32,8 +30,8 @@ describe Project do ...@@ -32,8 +30,8 @@ describe Project do
describe "admin access" do describe "admin access" do
before do before do
@p1.users_projects.create(:project => @p1, :user => @u1, :project_access => Project::PROJECT_RW) @p1.users_projects.create(:project => @p1, :user => @u1, :project_access => UsersProject::DEVELOPER)
@p1.users_projects.create(:project => @p1, :user => @u2, :project_access => Project::PROJECT_RWA) @p1.users_projects.create(:project => @p1, :user => @u2, :project_access => UsersProject::MASTER)
end end
it { @abilities.allowed?(@u1, :admin_project, @p1).should be_false } it { @abilities.allowed?(@u1, :admin_project, @p1).should be_false }
......
...@@ -20,11 +20,9 @@ describe "Projects" do ...@@ -20,11 +20,9 @@ describe "Projects" do
@u2 = Factory :user @u2 = Factory :user
@u3 = Factory :user @u3 = Factory :user
# full access # full access
@project.users_projects.create(:user => @u1, :project_access => Project::PROJECT_RWA) @project.users_projects.create(:user => @u1, :project_access => UsersProject::MASTER)
# no access
@project.users_projects.create(:user => @u2, :project_access => Project::PROJECT_N)
# readonly # readonly
@project.users_projects.create(:user => @u3, :project_access => Project::PROJECT_R) @project.users_projects.create(:user => @u3, :project_access => UsersProject::REPORTER)
end end
describe "GET /project_code" do describe "GET /project_code" do
......
...@@ -31,8 +31,7 @@ describe "TeamMembers" do ...@@ -31,8 +31,7 @@ describe "TeamMembers" do
before do before do
within "#new_team_member" do within "#new_team_member" do
select @user_1.name, :from => "team_member_user_id" select @user_1.name, :from => "team_member_user_id"
select "Report", :from => "team_member_project_access" select "Reporter", :from => "team_member_project_access"
select "Pull", :from => "team_member_repo_access"
end end
end end
...@@ -45,8 +44,7 @@ describe "TeamMembers" do ...@@ -45,8 +44,7 @@ describe "TeamMembers" do
page.should have_content @user_1.name page.should have_content @user_1.name
@member.reload @member.reload
@member.project_access.should == Project::PROJECT_RW @member.project_access.should == UsersProject::REPORTER
@member.repo_access.should == Repository::REPO_R
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment