[ADD]组织、组织团队

This commit is contained in:
2021-01-15 14:55:52 +08:00
parent c278ed1863
commit 7f309edc91
37 changed files with 667 additions and 57 deletions

View File

@@ -680,7 +680,7 @@ class ApplicationController < ActionController::Base
relation.page(page).per(limit)
end
def kaminary_array_paginate(relation)
def kaminari_array_paginate(relation)
limit = params[:limit] || params[:per_page]
limit = (limit.to_i.zero? || limit.to_i > 15) ? 15 : limit.to_i
page = params[:page].to_i.zero? ? 1 : params[:page].to_i

View File

@@ -1,22 +1,25 @@
class Organizations::BaseController < ApplicationController
include ApplicationHelper
def load_organization
@organization = Organization.find_by(login: params[:id]) || Organization.find_by(id: params[:id])
protected
@organization = nil if limited_condition || privacy_condition
render_not_found if @organization.nil?
@organization
def organization_owner
@organization.team_users.joins(:team).where(teams: {authorize: 'owner'}).take.user
end
private
def limited_condition
def org_limited_condition
@organization.organization_extension.limited? && !current_user.logged?
end
def privacy_condition
def org_privacy_condition
@organization.organization_extension.privacy? && @organization.organization_users.where(user_id: current_user.id).blank?
end
def team_not_found_condition
@team.team_users.where(user_id: current_user.id).blank? && !@organization.is_owner?(current_user)
end
def user_mark
params[:username] || params[:id]
end
end

View File

@@ -0,0 +1,55 @@
class Organizations::OrganizationUsersController < Organizations::BaseController
before_action :load_organization
before_action :load_operate_user, only: [:destroy]
before_action :load_organization_user, only: [:destroy]
def index
@organization_users = @organization.organization_users.includes(:user)
@organization_users = kaminari_paginate(@organization_users)
end
def destroy
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
ActiveRecord::Base.transaction do
@organization_user.destroy!
TeamUser.where(organization_id: @organization.id, user_id: @operate_user.id).map{|u| u.destroy!}
Gitea::Organization::OrganizationUser::DeleteService.call(current_user.gitea_token, @organization.login, @operate_user.login)
render_ok
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def quit
@organization_user = @organization.organization_users.find_by(user_id: current_user.id)
tip_exception("您不在该组织中") if @organization_user.nil?
ActiveRecord::Base.transaction do
@organization_user.destroy!
TeamUser.where(organization_id: @organization.id, user_id: current_user.id).map{|u| u.destroy!}
Gitea::Organization::OrganizationUser::DeleteService.call(organization_owner.gitea_token, @organization.login, current_user.login)
render_ok
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
private
def load_organization
@organization = Organization.find_by(login: params[:organization_id]) || Organization.find_by(id: params[:organization_id])
tip_exception("组织不存在") if @organization.nil?
tip_exception("没有查看组织的权限") if org_limited_condition || org_privacy_condition
end
def load_operate_user
@operate_user = User.find_by(login: user_mark) if user_mark.present?
tip_exception("平台用户不存在") if @operate_user.nil?
end
def load_organization_user
@organization_user = OrganizationUser.find_by(organization_id: @organization.id, user_id: @operate_user.id)
tip_exception("组织成员不存在") if @organization_user.nil?
end
end

View File

@@ -5,17 +5,17 @@ class Organizations::OrganizationsController < Organizations::BaseController
def index
if current_user.logged?
@organizations = Organization.with_visibility(%w(common limited)) +
Organization.with_visibility("privacy").joins(:organization_users).where(organization_users: {user_id: current_user.id})
kaminary_array_paginate(@organizations)
logged_organizations_sql = Organization.with_visibility(%w(common limited)).to_sql
privacy_organizations_sql = Organization.with_visibility("privacy").joins(:organization_users).where(organization_users: {user_id: current_user.id}).to_sql
@organizations = Organization.from("( #{ logged_organizations_sql } UNION #{ privacy_organizations_sql } ) AS users")
else
@organizations = Organization.with_visibility("common")
kaminari_paginate(@organizations)
end
@organizations = @organizations.includes(:organization_extension).order(id: :asc)
@organizations = kaminari_paginate(@organizations)
end
def show
end
def create
@@ -29,11 +29,12 @@ class Organizations::OrganizationsController < Organizations::BaseController
end
def update
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
ActiveRecord::Base.transaction do
login = @organization.login
@organization.update!(login: organization_params[:name]) if organization_params[:name].present?
@organization.organization_extension.update_attributes!(organization_params.except(:name))
Gitea::Organization::UpdateService.call(current_user.gitea_token, login, @organization)
Gitea::Organization::UpdateService.call(current_user.gitea_token, login, @organization.reload)
Util.write_file(@image, avatar_path(@organization)) if params[:image].present?
end
rescue Exception => e
@@ -42,8 +43,8 @@ class Organizations::OrganizationsController < Organizations::BaseController
end
def destroy
render_unauthorized unless current_user.check_password?(password)
render_forbidden("您没有权限进行该操作") unless @organization.check_owner?(current_user)
tip_exception("密码不正确") unless current_user.check_password?(password)
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
ActiveRecord::Base.transaction do
Gitea::Organization::DeleteService.call(current_user.gitea_token, @organization.login)
@organization.destroy!
@@ -76,4 +77,10 @@ class Organizations::OrganizationsController < Organizations::BaseController
params.fetch(:password, "")
end
def load_organization
@organization = Organization.find_by(login: params[:id]) || Organization.find_by(id: params[:id])
tip_exception("组织不存在") if @organization.nil?
tip_exception("没有查看组织的权限") if org_limited_condition || org_privacy_condition
end
end

View File

@@ -0,0 +1,74 @@
class Organizations::TeamUsersController < Organizations::BaseController
before_action :load_organization, :load_team
before_action :load_operate_user, only: [:create, :destroy]
before_action :load_team_user, only: [:destroy]
def index
@team_users = @team.team_users
@team_users = kaminari_paginate(@team_users)
end
def create
render_forbidden("您没有权限进行该操作") unless @organization.is_owner?(current_user)
ActiveRecord::Base.transaction do
@team_user = TeamUser.build(@organization.id, @operate_user.id, @team.id)
@organization_user = OrganizationUser.build(@organization.id, @operate_user.id)
Gitea::Organization::TeamUser::CreateService.call(current_user.gitea_token, @team.gtid, @operate_user.login)
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def destroy
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
tip_exception("您不能从 Owner 团队中删除最后一个用户") if @team.owner? && @team.num_users == 1
ActiveRecord::Base.transaction do
@team_user.destroy!
Gitea::Organization::TeamUser::DeleteService.call(current_user.gitea_token, @team.gtid, @operate_user.login)
render_ok
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def quit
@team_user = @team.team_users.find_by(user_id: current_user.id)
tip_exception("您不在该组织团队中") if @team_user.nil?
tip_exception("您不能从 Owner 团队中删除最后一个用户") if @team.owner? && @team.num_users == 1
ActiveRecord::Base.transaction do
@team_user.destroy!
Gitea::Organization::TeamUser::DeleteService.call(organization_owner.gitea_token, @team.gtid, current_user.login)
render_ok
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
private
def load_organization
@organization = Organization.find_by(login: params[:organization_id]) || Organization.find_by(id: params[:organization_id])
tip_exception("组织不存在") if @organization.nil?
tip_exception("没有查看组织的权限") if org_limited_condition || org_privacy_condition
end
def load_team
@team = Team.find_by_id(params[:team_id])
tip_exception("组织团队不存在") if @team.nil?
tip_exception("没有查看组织团队的权限") if team_not_found_condition
end
def load_operate_user
@operate_user = User.find_by(login: user_mark) if user_mark.present?
tip_exception("平台用户不存在") if @operate_user.nil?
end
def load_team_user
@team_user = TeamUser.find_by(team_id: @team.id, user_id: @operate_user.id)
tip_exception("组织团队成员不存在") if @team_user.nil?
end
end

View File

@@ -0,0 +1,62 @@
class Organizations::TeamsController < Organizations::BaseController
before_action :load_organization
before_action :load_team, only: [:show, :update, :destroy]
def index
if @organization.is_owner?(current_user)
@teams = @organization.teams
else
@teams = @organization.teams.joins(:team_users).where(team_users: {user_id: current_user.id})
end
@teams = kaminari_paginate(@teams)
end
def show
end
def create
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
@team = Organizations::Teams::CreateService.call(current_user, @organization, team_params)
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def update
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
@team = Organizations::Teams::UpdateService.call(current_user, @team, team_params)
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def destroy
tip_exception("您没有权限进行该操作") unless @organization.is_owner?(current_user)
ActiveRecord::Base.transaction do
Gitea::Organization::Team::DeleteService.call(current_user.gitea_token, @team.gtid)
@team.destroy!
end
render_ok
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
private
def team_params
params.permit(:name, :description, :authorize, :includes_all_project, :can_create_org_project, :unit_types => [])
end
def load_organization
@organization = Organization.find_by(login: params[:organization_id]) || Organization.find_by(id: params[:organization_id])
tip_exception("组织不存在") if @organization.nil?
tip_exception("没有查看组织的权限") if org_limited_condition || org_privacy_condition
end
def load_team
@team = Team.find_by_id(params[:id])
tip_exception("组织团队不存在") if @team.nil?
tip_exception("没有查看组织团队的权限") if team_not_found_condition
end
end

View File

@@ -0,0 +1,14 @@
class Users::OrganizationsController < Users::BaseController
def index
if current_user.logged?
logged_organizations_sql = observed_user.organizations.with_visibility(%w(common limited)).to_sql
privacy_organizations_sql = observed_user.organizations.with_visibility("privacy").joins(:organization_users).where(organization_users: {user_id: current_user.id}).to_sql
@organizations = Organization.from("( #{ logged_organizations_sql } UNION #{ privacy_organizations_sql } ) AS users")
else
@organizations = observed_user.organizations.with_visibility("common")
end
@organizations = @organizations.includes(:organization_extension).order(id: :asc)
end
end