Unverified Commit c69b8e04 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Huge replace of old users_project and users_group references


Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent eb4a9bfb
......@@ -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
class GroupMembers
constructor: ->
$('li.users_group').bind 'ajax:success', ->
$('li.group_member').bind 'ajax:success', ->
$(this).fadeOut()
@GroupMembers = GroupMembers
......
......@@ -8,7 +8,7 @@ def index
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 @@ def update
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
......
......@@ -16,10 +16,10 @@ def index
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
......
......@@ -65,15 +65,15 @@ def issues
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
......
......@@ -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.")
......
......@@ -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 @@ def update
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
......
......@@ -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 @@ def create
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 @@ def update
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 @@ def destroy
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 @@ def member
end
def member_params
params.require(:team_member).permit(:user_id, :project_access)
params.require(:team_member).permit(:user_id, :access_level)
end
end
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 @@ def authorize_admin_group!
end
def member_params
params.require(:users_group).permit(:group_access, :user_id)
params.require(:users_group).permit(:access_level, :user_id)
end
end
......@@ -19,10 +19,8 @@ def group_projects(current_user, group)
# 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
......
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,
......
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,
......
......@@ -14,7 +14,7 @@ def allowed(user, subject)
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
......
# == Notifiable concern
#
# Contains notification functionality shared between UsersProject and UsersGroup
# Contains notification functionality
#
module Notifiable
extend ActiveSupport::Concern
......
......@@ -17,8 +17,8 @@
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 @@ def human_name
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 @@ def last_owner?(user)
end
def members
users_groups
group_members
end
def avatar_type
......
class GroupMember < Member
end
......@@ -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
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
# == 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
class ProjectMember < Member
SOURCE_TYPE = 'Project'
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) }
include Gitlab::ShellAdapter
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) }
# 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
......@@ -50,7 +27,7 @@ class << self
# add_users_into_projects(
# project_ids,
# user_ids,
# UsersProject::MASTER
# ProjectMember::MASTER
# )
#
# add_users_into_projects(
......@@ -60,20 +37,20 @@ class << self
# )
#
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
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|
users_project = UsersProject.new(project_access: project_access, user_id: user_id)
users_project.project_id = project_id
users_project.save
member = ProjectMember.new(access_level: access_level, user_id: user_id)
member.source_id = project_id
member.save
end
end
end
......@@ -84,10 +61,10 @@ def add_users_into_projects(project_ids, user_ids, access)
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
ProjectMember.transaction do
members = ProjectMember.where(source_id: project_ids)
members.each do |member|
member.destroy
end
end
......@@ -110,7 +87,7 @@ def access_roles
end
def access_field
project_access
access_level
end
def owner?
......@@ -129,7 +106,7 @@ def post_create_hook
end
def post_update_hook
notification_service.update_team_member(self) if self.project_access_changed?
notification_service.update_team_member(self) if self.access_level_changed?
end
def post_destroy_hook
......@@ -149,4 +126,8 @@ def notification_service
def system_hook_service
SystemHooksService.new
end
def project
source
end
end
class ProjectMember < Member
end
......@@ -32,12 +32,12 @@ def find(user_id)
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 @@ def add_users(users, access)
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 @@ def add_users_ids(user_ids, access)
# Remove all users from project team
def truncate
UsersProject.truncate_team(project)
ProjectMember.truncate_team(project)
end
def users
......@@ -91,8 +91,8 @@ def masters
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 @@ def import(source_project)
new_tm
end
UsersProject.transaction do
ProjectMember.transaction do
source_team.each do |tm|
tm.save
end
......@@ -135,10 +135,10 @@ def master?(user)
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 @@ def max_tm_access(user_id)
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)
......
......@@ -81,10 +81,12 @@ class User < ActiveRecord::Base