diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-09-14 20:32:51 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-09-14 20:32:51 +0400 |
commit | c69b8e04591907d030b2cd544a606fd0e576116a (patch) | |
tree | 317db4259219bc7f3a3ff6e3c38c802a8d086d2c /app | |
parent | eb4a9bfbca5427e6b9a8fcf2ed0b1f9f96ae099d (diff) |
Huge replace of old users_project and users_group references
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app')
37 files changed, 277 insertions, 322 deletions
diff --git a/app/assets/javascripts/admin.js.coffee b/app/assets/javascripts/admin.js.coffee index 6634bb6cc34..a333eed87f2 100644 --- a/app/assets/javascripts/admin.js.coffee +++ b/app/assets/javascripts/admin.js.coffee @@ -46,10 +46,10 @@ class Admin modal.hide() $('.change-owner-link').show() - $('li.users_project').bind 'ajax:success', -> + $('li.project_member').bind 'ajax:success', -> Turbolinks.visit(location.href) - $('li.users_group').bind 'ajax:success', -> + $('li.group_member').bind 'ajax:success', -> Turbolinks.visit(location.href) @Admin = Admin diff --git a/app/assets/javascripts/groups.js.coffee b/app/assets/javascripts/groups.js.coffee index 49d6605980b..4b1000f9a6a 100644 --- a/app/assets/javascripts/groups.js.coffee +++ b/app/assets/javascripts/groups.js.coffee @@ -1,6 +1,6 @@ class GroupMembers constructor: -> - $('li.users_group').bind 'ajax:success', -> + $('li.group_member').bind 'ajax:success', -> $(this).fadeOut() @GroupMembers = GroupMembers diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb index 0388997ec69..e6d0c9323c1 100644 --- a/app/controllers/admin/groups_controller.rb +++ b/app/controllers/admin/groups_controller.rb @@ -8,7 +8,7 @@ class Admin::GroupsController < Admin::ApplicationController end def show - @members = @group.members.order("group_access DESC").page(params[:members_page]).per(30) + @members = @group.members.order("access_level DESC").page(params[:members_page]).per(30) @projects = @group.projects.page(params[:projects_page]).per(30) end @@ -40,7 +40,7 @@ class Admin::GroupsController < Admin::ApplicationController end def project_teams_update - @group.add_users(params[:user_ids].split(','), params[:group_access]) + @group.add_users(params[:user_ids].split(','), params[:access_level]) redirect_to [:admin, @group], notice: 'Users were successfully added.' end diff --git a/app/controllers/admin/projects_controller.rb b/app/controllers/admin/projects_controller.rb index 1c7c09d0cd4..2f0d344802f 100644 --- a/app/controllers/admin/projects_controller.rb +++ b/app/controllers/admin/projects_controller.rb @@ -16,10 +16,10 @@ class Admin::ProjectsController < Admin::ApplicationController def show if @group - @group_members = @group.members.order("group_access DESC").page(params[:group_members_page]).per(30) + @group_members = @group.members.order("access_level DESC").page(params[:group_members_page]).per(30) end - @project_members = @project.users_projects.page(params[:project_members_page]).per(30) + @project_members = @project.project_members.page(params[:project_members_page]).per(30) end def transfer diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index ddde90d3ee0..36222758eb2 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -65,15 +65,15 @@ class GroupsController < ApplicationController def members @project = group.projects.find(params[:project_id]) if params[:project_id] - @members = group.users_groups + @members = group.group_members if params[:search].present? users = group.users.search(params[:search]).to_a @members = @members.where(user_id: users) end - @members = @members.order('group_access DESC').page(params[:page]).per(50) - @users_group = UsersGroup.new + @members = @members.order('access_level DESC').page(params[:page]).per(50) + @users_group = GroupMember.new end def edit diff --git a/app/controllers/profiles/groups_controller.rb b/app/controllers/profiles/groups_controller.rb index 9a4d088651e..ce9dd50df67 100644 --- a/app/controllers/profiles/groups_controller.rb +++ b/app/controllers/profiles/groups_controller.rb @@ -2,11 +2,11 @@ class Profiles::GroupsController < ApplicationController layout "profile" def index - @user_groups = current_user.users_groups.page(params[:page]).per(20) + @user_groups = current_user.group_members.page(params[:page]).per(20) end def leave - @users_group = group.users_groups.where(user_id: current_user.id).first + @users_group = group.group_members.where(user_id: current_user.id).first if can?(current_user, :destroy, @users_group) @users_group.destroy redirect_to(profile_groups_path, info: "You left #{group.name} group.") diff --git a/app/controllers/profiles/notifications_controller.rb b/app/controllers/profiles/notifications_controller.rb index 5c492aeb49d..936fe071347 100644 --- a/app/controllers/profiles/notifications_controller.rb +++ b/app/controllers/profiles/notifications_controller.rb @@ -3,8 +3,8 @@ class Profiles::NotificationsController < ApplicationController def show @notification = current_user.notification - @users_projects = current_user.users_projects - @users_groups = current_user.users_groups + @users_projects = current_user.project_members + @users_groups = current_user.group_members end def update @@ -14,11 +14,11 @@ class Profiles::NotificationsController < ApplicationController current_user.notification_level = params[:notification_level] current_user.save elsif type == 'group' - users_group = current_user.users_groups.find(params[:notification_id]) + users_group = current_user.group_members.find(params[:notification_id]) users_group.notification_level = params[:notification_level] users_group.save else - users_project = current_user.users_projects.find(params[:notification_id]) + users_project = current_user.project_members.find(params[:notification_id]) users_project.notification_level = params[:notification_level] users_project.save end diff --git a/app/controllers/projects/team_members_controller.rb b/app/controllers/projects/team_members_controller.rb index 1de5bac9ee8..575797d36dc 100644 --- a/app/controllers/projects/team_members_controller.rb +++ b/app/controllers/projects/team_members_controller.rb @@ -6,17 +6,17 @@ class Projects::TeamMembersController < Projects::ApplicationController def index @group = @project.group - @users_projects = @project.users_projects.order('project_access DESC') + @users_projects = @project.project_members.order('access_level DESC') end def new - @user_project_relation = project.users_projects.new + @user_project_relation = project.project_members.new end def create users = User.where(id: params[:user_ids].split(',')) - @project.team << [users, params[:project_access]] + @project.team << [users, params[:access_level]] if params[:redirect_to] redirect_to params[:redirect_to] @@ -26,7 +26,7 @@ class Projects::TeamMembersController < Projects::ApplicationController end def update - @user_project_relation = project.users_projects.find_by(user_id: member) + @user_project_relation = project.project_members.find_by(user_id: member) @user_project_relation.update_attributes(member_params) unless @user_project_relation.valid? @@ -36,7 +36,7 @@ class Projects::TeamMembersController < Projects::ApplicationController end def destroy - @user_project_relation = project.users_projects.find_by(user_id: member) + @user_project_relation = project.project_members.find_by(user_id: member) @user_project_relation.destroy respond_to do |format| @@ -46,7 +46,7 @@ class Projects::TeamMembersController < Projects::ApplicationController end def leave - project.users_projects.find_by(user_id: current_user).destroy + project.project_members.find_by(user_id: current_user).destroy respond_to do |format| format.html { redirect_to :back } @@ -69,6 +69,6 @@ class Projects::TeamMembersController < Projects::ApplicationController end def member_params - params.require(:team_member).permit(:user_id, :project_access) + params.require(:team_member).permit(:user_id, :access_level) end end diff --git a/app/controllers/users_groups_controller.rb b/app/controllers/users_groups_controller.rb index a35a12a866b..07bd41d25eb 100644 --- a/app/controllers/users_groups_controller.rb +++ b/app/controllers/users_groups_controller.rb @@ -1,4 +1,4 @@ -class UsersGroupsController < ApplicationController +class GroupMembersController < ApplicationController before_filter :group # Authorize @@ -7,18 +7,18 @@ class UsersGroupsController < ApplicationController layout 'group' def create - @group.add_users(params[:user_ids].split(','), params[:group_access]) + @group.add_users(params[:user_ids].split(','), params[:access_level]) redirect_to members_group_path(@group), notice: 'Users were successfully added.' end def update - @member = @group.users_groups.find(params[:id]) + @member = @group.group_members.find(params[:id]) @member.update_attributes(member_params) end def destroy - @users_group = @group.users_groups.find(params[:id]) + @users_group = @group.group_members.find(params[:id]) if can?(current_user, :destroy, @users_group) # May fail if last owner. @users_group.destroy respond_to do |format| @@ -43,6 +43,6 @@ class UsersGroupsController < ApplicationController end def member_params - params.require(:users_group).permit(:group_access, :user_id) + params.require(:users_group).permit(:access_level, :user_id) end end diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb index 26898bad493..222496cd317 100644 --- a/app/finders/projects_finder.rb +++ b/app/finders/projects_finder.rb @@ -19,10 +19,8 @@ class ProjectsFinder # Return ALL group projects group.projects else - projects_members = UsersProject.where( - project_id: group.projects, - user_id: current_user - ) + projects_members = ProjectMember.in_projects(group.projects). + with_user(current_user) if projects_members.any? # User is a project member diff --git a/app/mailers/emails/groups.rb b/app/mailers/emails/groups.rb index 1654fc55bca..3f7c1d27350 100644 --- a/app/mailers/emails/groups.rb +++ b/app/mailers/emails/groups.rb @@ -1,7 +1,7 @@ module Emails module Groups - def group_access_granted_email(user_group_id) - @membership = UsersGroup.find(user_group_id) + def access_level_granted_email(user_group_id) + @membership = GroupMember.find(user_group_id) @group = @membership.group @target_url = group_url(@group) mail(to: @membership.user.email, diff --git a/app/mailers/emails/projects.rb b/app/mailers/emails/projects.rb index 6b13a1d746d..152d5aa9959 100644 --- a/app/mailers/emails/projects.rb +++ b/app/mailers/emails/projects.rb @@ -1,7 +1,7 @@ module Emails module Projects - def project_access_granted_email(user_project_id) - @users_project = UsersProject.find user_project_id + def access_level_granted_email(user_project_id) + @users_project = ProjectMember.find user_project_id @project = @users_project.project @target_url = project_url(@project) mail(to: @users_project.user.email, diff --git a/app/models/ability.rb b/app/models/ability.rb index f1d57de63bb..716a23a4284 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -14,7 +14,7 @@ class Ability when "MergeRequest" then merge_request_abilities(user, subject) when "Group" then group_abilities(user, subject) when "Namespace" then namespace_abilities(user, subject) - when "UsersGroup" then users_group_abilities(user, subject) + when "GroupMember" then users_group_abilities(user, subject) else [] end.concat(global_abilities(user)) end diff --git a/app/models/concerns/notifiable.rb b/app/models/concerns/notifiable.rb index 722f375e71d..d7dcd97911d 100644 --- a/app/models/concerns/notifiable.rb +++ b/app/models/concerns/notifiable.rb @@ -1,6 +1,6 @@ # == Notifiable concern # -# Contains notification functionality shared between UsersProject and UsersGroup +# Contains notification functionality # module Notifiable extend ActiveSupport::Concern diff --git a/app/models/group.rb b/app/models/group.rb index 66239f7fe6f..b8ed3b8ac73 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -17,8 +17,8 @@ require 'carrierwave/orm/activerecord' require 'file_size_validator' class Group < Namespace - has_many :users_groups, dependent: :destroy - has_many :users, through: :users_groups + has_many :group_members, dependent: :destroy, as: :source, class_name: 'GroupMember' + has_many :users, through: :group_members validate :avatar_type, if: ->(user) { user.avatar_changed? } validates :avatar, file_size: { maximum: 100.kilobytes.to_i } @@ -30,22 +30,22 @@ class Group < Namespace end def owners - @owners ||= users_groups.owners.map(&:user) + @owners ||= group_members.owners.map(&:user) end - def add_users(user_ids, group_access) + def add_users(user_ids, access_level) user_ids.compact.each do |user_id| - user = self.users_groups.find_or_initialize_by(user_id: user_id) - user.update_attributes(group_access: group_access) + user = self.group_members.find_or_initialize_by(user_id: user_id) + user.update_attributes(access_level: access_level) end end - def add_user(user, group_access) - self.users_groups.create(user_id: user.id, group_access: group_access) + def add_user(user, access_level) + self.group_members.create(user_id: user.id, access_level: access_level) end def add_owner(user) - self.add_user(user, UsersGroup::OWNER) + self.add_user(user, Gitlab::Access::OWNER) end def has_owner?(user) @@ -61,7 +61,7 @@ class Group < Namespace end def members - users_groups + group_members end def avatar_type diff --git a/app/models/group_member.rb b/app/models/group_member.rb deleted file mode 100644 index 88c207deef9..00000000000 --- a/app/models/group_member.rb +++ /dev/null @@ -1,2 +0,0 @@ -class GroupMember < Member -end diff --git a/app/models/member.rb b/app/models/member.rb index 13827db175b..7dc13c18bf3 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -8,11 +8,13 @@ class Member < ActiveRecord::Base validates :user, presence: true validates :source, presence: true validates :user_id, uniqueness: { scope: [:source_type, :source_id], message: "already exists in source" } - validates :access_level, inclusion: { in: Gitlab::Access.values }, presence: true + validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true - scope :guests, -> { where(group_access: GUEST) } - scope :reporters, -> { where(group_access: REPORTER) } - scope :developers, -> { where(group_access: DEVELOPER) } - scope :masters, -> { where(group_access: MASTER) } - scope :owners, -> { where(group_access: OWNER) } + scope :guests, -> { where(access_level: GUEST) } + scope :reporters, -> { where(access_level: REPORTER) } + scope :developers, -> { where(access_level: DEVELOPER) } + scope :masters, -> { where(access_level: MASTER) } + scope :owners, -> { where(access_level: OWNER) } + + delegate :name, :username, :email, to: :user, prefix: true end diff --git a/app/models/members/group_member.rb b/app/models/members/group_member.rb new file mode 100644 index 00000000000..7f68662de70 --- /dev/null +++ b/app/models/members/group_member.rb @@ -0,0 +1,40 @@ +class GroupMember < Member + SOURCE_TYPE = 'Group' + + # Make sure group member points only to group as it source + default_value_for :source_type, SOURCE_TYPE + validates_format_of :source_type, with: /\AGroup\z/ + default_scope { where(source_type: SOURCE_TYPE) } + + scope :with_group, ->(group) { where(source_id: group.id) } + scope :with_user, ->(user) { where(user_id: user.id) } + + after_create :notify_create + after_update :notify_update + + def self.access_level_roles + Gitlab::Access.options_with_owner + end + + def group + source + end + + def access_field + access_level + end + + def notify_create + notification_service.new_group_member(self) + end + + def notify_update + if access_level_changed? + notification_service.update_group_member(self) + end + end + + def notification_service + NotificationService.new + end +end diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb new file mode 100644 index 00000000000..403b87b7e36 --- /dev/null +++ b/app/models/members/project_member.rb @@ -0,0 +1,133 @@ +class ProjectMember < Member + SOURCE_TYPE = 'Project' + + include Gitlab::ShellAdapter + + # Make sure project member points only to project as it source + default_value_for :source_type, SOURCE_TYPE + validates_format_of :source_type, with: /\AProject\z/ + default_scope { where(source_type: SOURCE_TYPE) } + + after_create :post_create_hook + after_update :post_update_hook + after_destroy :post_destroy_hook + + scope :in_project, ->(project) { where(source_id: project.id) } + scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) } + scope :with_user, ->(user) { where(user_id: user.id) } + + class << self + + # Add users to project teams with passed access option + # + # access can be an integer representing a access code + # or symbol like :master representing role + # + # Ex. + # add_users_into_projects( + # project_ids, + # user_ids, + # ProjectMember::MASTER + # ) + # + # add_users_into_projects( + # project_ids, + # user_ids, + # :master + # ) + # + def add_users_into_projects(project_ids, user_ids, access) + access_level = if roles_hash.has_key?(access) + roles_hash[access] + elsif roles_hash.values.include?(access.to_i) + access + else + raise "Non valid access" + end + + ProjectMember.transaction do + project_ids.each do |project_id| + user_ids.each do |user_id| + member = ProjectMember.new(access_level: access_level, user_id: user_id) + member.source_id = project_id + member.save + end + end + end + + true + rescue + false + end + + def truncate_teams(project_ids) + ProjectMember.transaction do + members = ProjectMember.where(source_id: project_ids) + members.each do |member| + member.destroy + end + end + + true + rescue + false + end + + def truncate_team project + truncate_teams [project.id] + end + + def roles_hash + Gitlab::Access.sym_options + end + + def access_roles + Gitlab::Access.options + end + end + + def access_field + access_level + end + + def owner? + project.owner == user + end + + def post_create_hook + Event.create( + project_id: self.project.id, + action: Event::JOINED, + author_id: self.user.id + ) + + notification_service.new_team_member(self) unless owner? + system_hook_service.execute_hooks_for(self, :create) + end + + def post_update_hook + notification_service.update_team_member(self) if self.access_level_changed? + end + + def post_destroy_hook + Event.create( + project_id: self.project.id, + action: Event::LEFT, + author_id: self.user.id + ) + + system_hook_service.execute_hooks_for(self, :destroy) + end + + def notification_service + NotificationService.new + end + + def system_hook_service + SystemHooksService.new + end + + def project + source + end +end diff --git a/app/models/project_member.rb b/app/models/project_member.rb deleted file mode 100644 index 67f65285560..00000000000 --- a/app/models/project_member.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ProjectMember < Member -end diff --git a/app/models/project_team.rb b/app/models/project_team.rb index 0bbbd3d00e8..330e2aa728c 100644 --- a/app/models/project_team.rb +++ b/app/models/project_team.rb @@ -32,12 +32,12 @@ class ProjectTeam end def find_tm(user_id) - tm = project.users_projects.find_by(user_id: user_id) + tm = project.project_members.find_by(user_id: user_id) # If user is not in project members # we should check for group membership if group && !tm - tm = group.users_groups.find_by(user_id: user_id) + tm = group.group_members.find_by(user_id: user_id) end tm @@ -52,7 +52,7 @@ class ProjectTeam end def add_users_ids(user_ids, access) - UsersProject.add_users_into_projects( + ProjectMember.add_users_into_projects( [project.id], user_ids, access @@ -61,7 +61,7 @@ class ProjectTeam # Remove all users from project team def truncate - UsersProject.truncate_team(project) + ProjectMember.truncate_team(project) end def users @@ -91,8 +91,8 @@ class ProjectTeam def import(source_project) target_project = project - source_team = source_project.users_projects.to_a - target_user_ids = target_project.users_projects.pluck(:user_id) + source_team = source_project.project_members.to_a + target_user_ids = target_project.project_members.pluck(:user_id) source_team.reject! do |tm| # Skip if user already present in team @@ -106,7 +106,7 @@ class ProjectTeam new_tm end - UsersProject.transaction do + ProjectMember.transaction do source_team.each do |tm| tm.save end @@ -135,10 +135,10 @@ class ProjectTeam def max_tm_access(user_id) access = [] - access << project.users_projects.find_by(user_id: user_id).try(:access_field) + access << project.project_members.find_by(user_id: user_id).try(:access_field) if group - access << group.users_groups.find_by(user_id: user_id).try(:access_field) + access << group.group_members.find_by(user_id: user_id).try(:access_field) end access.compact.max @@ -147,8 +147,8 @@ class ProjectTeam private def fetch_members(level = nil) - project_members = project.users_projects - group_members = group ? group.users_groups : [] + project_members = project.project_members + group_members = group ? group.group_members : [] if level project_members = project_members.send(level) diff --git a/app/models/user.rb b/app/models/user.rb index 15e56a62a68..b235437817a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -81,10 +81,12 @@ class User < ActiveRecord::Base has_many :emails, dependent: :destroy # Groups - has_many :users_groups, dependent: :destroy - has_many :groups, through: :users_groups - has_many :owned_groups, -> { where users_groups: { group_access: UsersGroup::OWNER } }, through: :users_groups, source: :group - has_many :masters_groups, -> { where users_groups: { group_access: UsersGroup::MASTER } }, through: :users_groups, source: :group + has_many :members, dependent: :destroy + has_many :project_members, source: 'ProjectMember' + has_many :group_members, source: 'GroupMember' + has_many :groups, through: :group_members + has_many :owned_groups, -> { where group_members: { access_level: Gitlab::Access::OWNER } }, through: :group_members, source: :group + has_many :masters_groups, -> { where group_members: { access_level: Gitlab::Access::MASTER } }, through: :group_members, source: :group # Projects has_many :groups_projects, through: :groups, source: :projects @@ -140,7 +142,7 @@ class User < ActiveRecord::Base state_machine :state, initial: :active do after_transition any => :blocked do |user, transition| # Remove user from all projects and - user.users_projects.find_each do |membership| + user.project_members.find_each do |membership| # skip owned resources next if membership.project.owner == user @@ -148,7 +150,7 @@ class User < ActiveRecord::Base end # Remove user from all groups - user.users_groups.find_each do |membership| + user.group_members.find_each do |membership| # skip owned resources next if membership.group.last_owner?(user) @@ -295,7 +297,7 @@ class User < ActiveRecord::Base # Team membership in authorized projects def tm_in_authorized_projects - UsersProject.where(project_id: authorized_projects.map(&:id), user_id: self.id) + ProjectMember.where(source_id: authorized_projects.map(&:id), user_id: self.id) end def is_admin? diff --git a/app/models/users_group.rb b/app/models/users_group.rb deleted file mode 100644 index 270f968ef61..00000000000 --- a/app/models/users_group.rb +++ /dev/null @@ -1,61 +0,0 @@ -# == Schema Information -# -# Table name: users_groups -# -# id :integer not null, primary key -# group_access :integer not null -# group_id :integer not null -# user_id :integer not null -# created_at :datetime -# updated_at :datetime -# notification_level :integer default(3), not null -# - -class UsersGroup < ActiveRecord::Base - include Notifiable - include Gitlab::Access - - def self.group_access_roles - Gitlab::Access.options_with_owner - end - - belongs_to :user - belongs_to :group - - scope :guests, -> { where(group_access: GUEST) } - scope :reporters, -> { where(group_access: REPORTER) } - scope :developers, -> { where(group_access: DEVELOPER) } - scope :masters, -> { where(group_access: MASTER) } - scope :owners, -> { where(group_access: OWNER) } - - scope :with_group, ->(group) { where(group_id: group.id) } - scope :with_user, ->(user) { where(user_id: user.id) } - - after_create :notify_create - after_update :notify_update - - validates :group_access, inclusion: { in: UsersGroup.group_access_roles.values }, presence: true - validates :user_id, presence: true - validates :group_id, presence: true - validates :user_id, uniqueness: { scope: [:group_id], message: "already exists in group" } - - delegate :name, :username, :email, to: :user, prefix: true - - def access_field - group_access - end - - def notify_create - notification_service.new_group_member(self) - end - - def notify_update - if group_access_changed? - notification_service.update_group_member(self) - end - end - - def notification_service - NotificationService.new - end -end diff --git a/app/models/users_project.rb b/app/models/users_project.rb deleted file mode 100644 index 60bdf7a3cfb..00000000000 --- a/app/models/users_project.rb +++ /dev/null @@ -1,152 +0,0 @@ -# == Schema Information -# -# Table name: users_projects -# -# id :integer not null, primary key -# user_id :integer not null -# project_id :integer not null -# created_at :datetime -# updated_at :datetime -# project_access :integer default(0), not null -# notification_level :integer default(3), not null -# - -class UsersProject < ActiveRecord::Base - include Gitlab::ShellAdapter - include Notifiable - include Gitlab::Access - - belongs_to :user - belongs_to :project - - validates :user, presence: true - validates :user_id, uniqueness: { scope: [:project_id], message: "already exists in project" } - validates :project_access, inclusion: { in: Gitlab::Access.values }, presence: true - validates :project, presence: true - - delegate :name, :username, :email, to: :user, prefix: true - - scope :guests, -> { where(project_access: GUEST) } - scope :reporters, -> { where(project_access: REPORTER) } - scope :developers, -> { where(project_access: DEVELOPER) } - scope :masters, -> { where(project_access: MASTER) } - - scope :in_project, ->(project) { where(project_id: project.id) } - scope :in_projects, ->(projects) { where(project_id: projects.map { |p| p.id }) } - scope :with_user, ->(user) { where(user_id: user.id) } - - after_create :post_create_hook - after_update :post_update_hook - after_destroy :post_destroy_hook - - class << self - - # Add users to project teams with passed access option - # - # access can be an integer representing a access code - # or symbol like :master representing role - # - # Ex. - # add_users_into_projects( - # project_ids, - # user_ids, - # UsersProject::MASTER - # ) - # - # add_users_into_projects( - # project_ids, - # user_ids, - # :master - # ) - # - def add_users_into_projects(project_ids, user_ids, access) - project_access = if roles_hash.has_key?(access) - roles_hash[access] - elsif roles_hash.values.include?(access.to_i) - access - else - raise "Non valid access" - end - - UsersProject.transaction do - project_ids.each do |project_id| - user_ids.each do |user_id| - users_project = UsersProject.new(project_access: project_access, user_id: user_id) - users_project.project_id = project_id - users_project.save - end - end - end - - true - rescue - false - end - - def truncate_teams(project_ids) - UsersProject.transaction do - users_projects = UsersProject.where(project_id: project_ids) - users_projects.each do |users_project| - users_project.destroy - end - end - - true - rescue - false - end - - def truncate_team project - truncate_teams [project.id] - end - - def roles_hash - Gitlab::Access.sym_options - end - - def access_roles - Gitlab::Access.options - end - end - - def access_field - project_access - end - - def owner? - project.owner == user - end - - def post_create_hook - Event.create( - project_id: self.project.id, - action: Event::JOINED, - author_id: self.user.id - ) - - notification_service.new_team_member(self) unless owner? - system_hook_service.execute_hooks_for(self, :create) - end - - def post_update_hook - notification_service.update_team_member(self) if self.project_access_changed? - end - - def post_destroy_hook - Event.create( - project_id: self.project.id, - action: Event::LEFT, - author_id: self.user.id - ) - - system_hook_service.execute_hooks_for(self, :destroy) - end - - def notification_service - NotificationService.new - end - - def system_hook_service - SystemHooksService.new - end -end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 36d33e0d7ca..ca483e2f109 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -158,19 +158,19 @@ class NotificationService end def new_team_member(users_project) - mailer.project_access_granted_email(users_project.id) + mailer.access_level_granted_email(users_project.id) end def update_team_member(users_project) - mailer.project_access_granted_email(users_project.id) + mailer.access_level_granted_email(users_project.id) end def new_group_member(users_group) - mailer.group_access_granted_email(users_group.id) + mailer.access_level_granted_email(users_group.id) end def update_group_member(users_group) - mailer.group_access_granted_email(users_group.id) + mailer.access_level_granted_email(users_group.id) end def project_was_moved(project) @@ -199,7 +199,7 @@ class NotificationService end def users_project_notification(project, notification_level=nil) - project_members = project.users_projects + project_members = project.project_members if notification_level project_members.where(notification_level: notification_level).pluck(:user_id) @@ -210,7 +210,7 @@ class NotificationService def users_group_notification(project, notification_level) if project.group - project.group.users_groups.where(notification_level: notification_level).pluck(:user_id) + project.group.group_members.where(notification_level: notification_level).pluck(:user_id) else [] end @@ -267,10 +267,10 @@ class NotificationService users.reject do |user| next user.notification.disabled? unless project - tm = project.users_projects.find_by(user_id: user.id) + tm = project.project_members.find_by(user_id: user.id) if !tm && project.group - tm = project.group.users_groups.find_by(user_id: user.id) + tm = project.group.group_members.find_by(user_id: user.id) end # reject users who globally disabled notification and has no membership diff --git a/app/services/projects/create_service.rb b/app/services/projects/create_service.rb index 2bfb0f28d95..12386792aab 100644 --- a/app/services/projects/create_service.rb +++ b/app/services/projects/create_service.rb @@ -42,10 +42,7 @@ module Projects system_hook_service.execute_hooks_for(@project, :create) unless @project.group - @project.users_projects.create( - project_access: UsersProject::MASTER, - user: current_user - ) + @project.team << [current_user, :master] end @project.update_column(:last_activity_at, @project.created_at) diff --git a/app/services/projects/fork_service.rb b/app/services/projects/fork_service.rb index 66f0a02f0af..a59311bf942 100644 --- a/app/services/projects/fork_service.rb +++ b/app/services/projects/fork_service.rb @@ -27,7 +27,7 @@ module Projects #First save the DB entries as they can be rolled back if the repo fork fails project.build_forked_project_link(forked_to_project_id: project.id, forked_from_project_id: @from_project.id) if project.save - project.users_projects.create(project_access: UsersProject::MASTER, user: current_user) + project.team << [current_user, :master] end #Now fork the repo unless gitlab_shell.fork_repository(@from_project.path_with_namespace, project.namespace.path) diff --git a/app/services/system_hooks_service.rb b/app/services/system_hooks_service.rb index bfc725e5eb5..7de85c51805 100644 --- a/app/services/system_hooks_service.rb +++ b/app/services/system_hooks_service.rb @@ -50,14 +50,14 @@ class SystemHooksService email: model.email, user_id: model.id }) - when UsersProject + when ProjectMember data.merge!({ project_name: model.project.name, project_path: model.project.path, project_id: model.project_id, user_name: model.user.name, user_email: model.user.email, - project_access: model.human_access, + access_level: model.human_access, project_visibility: Project.visibility_levels.key(model.project.visibility_level_field).downcase }) end @@ -65,7 +65,7 @@ class SystemHooksService def build_event_name(model, event) case model - when UsersProject + when ProjectMember return "user_add_to_team" if event == :create return "user_remove_from_team" if event == :destroy else diff --git a/app/views/admin/groups/show.html.haml b/app/views/admin/groups/show.html.haml index 8634f46053d..d7ccd67dac7 100644 --- a/app/views/admin/groups/show.html.haml +++ b/app/views/admin/groups/show.html.haml @@ -62,7 +62,7 @@ %div = users_select_tag(:user_ids, multiple: true) %div.prepend-top-10 - = select_tag :group_access, options_for_select(UsersGroup.group_access_roles), class: "project-access-select select2" + = select_tag :access_level, options_for_select(GroupMember.access_level_roles), class: "project-access-select select2" %hr = submit_tag 'Add users into group', class: "btn btn-create" .panel.panel-default @@ -70,7 +70,7 @@ %h3.panel-title Members %span.badge - #{@group.users_groups.count} + #{@group.group_members.count} %ul.well-list.group-users-list - @members.each do |member| - user = member.user diff --git a/app/views/admin/projects/show.html.haml b/app/views/admin/projects/show.html.haml index 66a72449f40..7a1fc06d3ac 100644 --- a/app/views/admin/projects/show.html.haml +++ b/app/views/admin/projects/show.html.haml @@ -95,7 +95,7 @@ .panel.panel-default .panel-heading %strong #{@group.name} - group members (#{@group.users_groups.count}) + group members (#{@group.group_members.count}) .pull-right = link_to admin_group_path(@group), class: 'btn btn-small' do %i.icon-edit @@ -117,7 +117,7 @@ %ul.well-list.team_members - @project_members.each do |users_project| - user = users_project.user - %li.users_project + %li.project_member .list-item-name %strong = link_to user.name, admin_user_path(user) diff --git a/app/views/admin/users/show.html.haml b/app/views/admin/users/show.html.haml index f60d40b5334..a172d12edb1 100644 --- a/app/views/admin/users/show.html.haml +++ b/app/views/admin/users/show.html.haml @@ -159,13 +159,13 @@ = render 'users/profile', user: @user #groups.tab-pane - - if @user.users_groups.present? + - if @user.group_members.present? .panel.panel-default .panel-heading Groups: %ul.well-list - - @user.users_groups.each do |user_group| + - @user.group_members.each do |user_group| - group = user_group.group - %li.users_group + %li.group_member %span{class: ("list-item-name" unless user_group.owner?)} %strong= link_to group.name, admin_group_path(group) .pull-right @@ -197,7 +197,7 @@ %ul.well-list - @joined_projects.sort_by(&:name_with_namespace).each do |project| - tm = project.team.find_tm(@user.id) - %li.users_project + %li.project_member .list-item-name = link_to admin_project_path(project), class: dom_class(project) do = project.name_with_namespace diff --git a/app/views/groups/_new_group_member.html.haml b/app/views/groups/_new_group_member.html.haml index 3ab9276c541..2938a15c608 100644 --- a/app/views/groups/_new_group_member.html.haml +++ b/app/views/groups/_new_group_member.html.haml @@ -4,8 +4,8 @@ .col-sm-10= users_select_tag(:user_ids, multiple: true, class: 'input-large') .form-group - = f.label :group_access, "Group Access", class: 'control-label' - .col-sm-10= select_tag :group_access, options_for_select(UsersGroup.group_access_roles, @users_group.group_access), class: "project-access-select select2" + = f.label :access_level, "Group Access", class: 'control-label' + .col-sm-10= select_tag :access_level, options_for_select(GroupMember.access_level_roles, @users_group.access_level), class: "project-access-select select2" .form-actions = f.submit 'Add users into group', class: "btn btn-create" diff --git a/app/views/profiles/notifications/_settings.html.haml b/app/views/profiles/notifications/_settings.html.haml index 218d51d31af..940c553d1ba 100644 --- a/app/views/profiles/notifications/_settings.html.haml +++ b/app/views/profiles/notifications/_settings.html.haml @@ -6,7 +6,7 @@ = notification_icon(notification) %span.str-truncated - - if membership.kind_of? UsersGroup + - if membership.kind_of? GroupMember = link_to membership.group.name, membership.group - else = link_to_project(membership.project) diff --git a/app/views/projects/team_members/_form.html.haml b/app/views/projects/team_members/_form.html.haml index 5998e4c6b42..7e3fe02e198 100644 --- a/app/views/projects/team_members/_form.html.haml +++ b/app/views/projects/team_members/_form.html.haml @@ -16,8 +16,8 @@ %p 2. Set access level for them .form-group - = f.label :project_access, "Project Access", class: 'control-label' - .col-sm-10= select_tag :project_access, options_for_select(Gitlab::Access.options, @user_project_relation.project_access), class: "project-access-select select2" + = f.label :access_level, "Project Access", class: 'control-label' + .col-sm-10= select_tag :access_level, options_for_select(Gitlab::Access.options, @user_project_relation.access_level), class: "project-access-select select2" .form-actions = f.submit 'Add users', class: "btn btn-create" diff --git a/app/views/projects/team_members/_group_members.html.haml b/app/views/projects/team_members/_group_members.html.haml index 83c4b6f87d5..c86648138c7 100644 --- a/app/views/projects/team_members/_group_members.html.haml +++ b/app/views/projects/team_members/_group_members.html.haml @@ -1,4 +1,4 @@ -- group_users_count = @group.users_groups.count +- group_users_count = @group.group_members.count .panel.panel-default .panel-heading %strong #{@group.name} @@ -7,7 +7,7 @@ = link_to members_group_path(@group), class: 'btn btn-small' do %i.icon-edit %ul.well-list - - @group.users_groups.order('group_access DESC').limit(20).each do |member| + - @group.group_members.order('access_level DESC').limit(20).each do |member| = render 'users_groups/users_group', member: member, show_controls: false - if group_users_count > 20 %li diff --git a/app/views/projects/team_members/_team_member.html.haml b/app/views/projects/team_members/_team_member.html.haml index d93bb44ab96..3ecbf129083 100644 --- a/app/views/projects/team_members/_team_member.html.haml +++ b/app/views/projects/team_members/_team_member.html.haml @@ -5,7 +5,7 @@ - unless @project.personal? && user == current_user .pull-left = form_for(member, as: :team_member, url: project_team_member_path(@project, member.user)) do |f| - = f.select :project_access, options_for_select(UsersProject.access_roles, member.project_access), {}, class: "medium project-access-select span2 trigger-submit" + = f.select :access_level, options_for_select(ProjectMember.access_roles, member.access_level), {}, class: "medium project-access-select span2 trigger-submit" = link_to project_team_member_path(@project, user), data: { confirm: remove_from_project_team_message(@project, user)}, method: :delete, class: "btn-tiny btn btn-remove", title: 'Remove user from team' do %i.icon-minus.icon-white diff --git a/app/views/users_groups/_users_group.html.haml b/app/views/users_groups/_users_group.html.haml index ad363eaba23..7e246d3b7fb 100644 --- a/app/views/users_groups/_users_group.html.haml +++ b/app/views/users_groups/_users_group.html.haml @@ -27,5 +27,5 @@ .edit-member.hide.js-toggle-content = form_for [@group, member], remote: true do |f| .alert.prepend-top-20 - = f.select :group_access, options_for_select(UsersGroup.group_access_roles, member.group_access) + = f.select :access_level, options_for_select(GroupMember.access_level_roles, member.access_level) = f.submit 'Save', class: 'btn btn-save btn-small' |