diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/api/api.rb | 1 | ||||
| -rw-r--r-- | lib/api/entities.rb | 4 | ||||
| -rw-r--r-- | lib/api/user_teams.rb | 276 | ||||
| -rw-r--r-- | lib/gitlab/user_team_manager.rb | 146 | 
4 files changed, 0 insertions, 427 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 5d97d50cb82..c4c9f166db1 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -35,7 +35,6 @@ module API      mount Notes      mount Internal      mount SystemHooks -    mount UserTeams      mount ProjectSnippets      mount DeployKeys      mount ProjectHooks diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 0d8cac5c8fd..14d6c0f9353 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -99,10 +99,6 @@ module API        expose :id, :title, :key, :created_at      end -    class UserTeam < Grape::Entity -      expose :id, :name, :path, :owner_id -    end -      class MergeRequest < Grape::Entity        expose :id, :target_branch, :source_branch, :project_id, :title, :state        expose :author, :assignee, using: Entities::UserBasic diff --git a/lib/api/user_teams.rb b/lib/api/user_teams.rb deleted file mode 100644 index 05aa72f0e92..00000000000 --- a/lib/api/user_teams.rb +++ /dev/null @@ -1,276 +0,0 @@ -module API -  # user_teams API -  class UserTeams < Grape::API -    before { authenticate! } - -    resource :user_teams do -      helpers do -        def handle_team_member_errors(errors) -          if errors[:permission].any? -            render_api_error!(errors[:permission], 422) -          end -          not_found! -        end - -        def validate_access_level?(level) -          [UsersProject::GUEST, UsersProject::REPORTER, UsersProject::DEVELOPER, UsersProject::MASTER].include? level.to_i -        end -      end - - -      # Get a user_teams list -      # -      # Example Request: -      #  GET /user_teams -      get do -        if current_user.admin -          @user_teams = paginate UserTeam -        else -          @user_teams = paginate current_user.user_teams -        end -        present @user_teams, with: Entities::UserTeam -      end - - -      # Create user_team. Available only for admin -      # -      # Parameters: -      #   name (required) - The name of the user_team -      #   path (required) - The path of the user_team -      # Example Request: -      #   POST /user_teams -      post do -        authenticated_as_admin! -        required_attributes! [:name, :path] - -        attrs = attributes_for_keys [:name, :path] -        @user_team = UserTeam.new(attrs) -        @user_team.owner = current_user - -        if @user_team.save -          present @user_team, with: Entities::UserTeam -        else -          not_found! -        end -      end - - -      # Get a single user_team -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      # Example Request: -      #   GET /user_teams/:id -      get ":id" do -        @user_team = UserTeam.find(params[:id]) -        if current_user.admin or current_user.user_teams.include? @user_team -          present @user_team, with: Entities::UserTeam -        else -          not_found! -        end -      end - - -      # Get user_team members -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      # Example Request: -      #   GET /user_teams/:id/members -      get ":id/members" do -        @user_team = UserTeam.find(params[:id]) -        if current_user.admin or current_user.user_teams.include? @user_team -          @members = paginate @user_team.members -          present @members, with: Entities::TeamMember, user_team: @user_team -        else -          not_found! -        end -      end - - -      # Add a new user_team member -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      #   user_id (required) - The ID of a user -      #   access_level (required) - Project access level -      # Example Request: -      #   POST /user_teams/:id/members -      post ":id/members" do -        authenticated_as_admin! -        required_attributes! [:user_id, :access_level] - -        if not validate_access_level?(params[:access_level]) -          render_api_error!("Wrong access level", 422) -        end - -        @user_team = UserTeam.find(params[:id]) -        if @user_team -          team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id]) -          # Not existing member -          if team_member.nil? -            @user_team.add_member(params[:user_id], params[:access_level], false) -            team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id]) - -            if team_member.nil? -              render_api_error!("Error creating membership", 500) -            else -              @member = team_member.user -              present @member, with: Entities::TeamMember, user_team: @user_team -            end -          else -            render_api_error!("Already exists", 409) -          end -        else -          not_found! -        end -      end - - -      # Get a single team member from user_team -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      #   user_id (required) - The ID of a team member -      # Example Request: -      #   GET /user_teams/:id/members/:user_id -      get ":id/members/:user_id" do -        @user_team = UserTeam.find(params[:id]) -        if current_user.admin or current_user.user_teams.include? @user_team -          team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id]) -          unless team_member.nil? -            present team_member.user, with: Entities::TeamMember, user_team: @user_team -          else -            not_found! -          end -        else -          not_found! -        end -      end - -      # Remove a team member from user_team -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      #   user_id (required) - The ID of a team member -      # Example Request: -      #   DELETE /user_teams/:id/members/:user_id -      delete ":id/members/:user_id" do -        authenticated_as_admin! - -        @user_team = UserTeam.find(params[:id]) -        if @user_team -          team_member = @user_team.user_team_user_relationships.find_by_user_id(params[:user_id]) -          unless team_member.nil? -            team_member.destroy -          else -            not_found! -          end -        else -          not_found! -        end -      end - - -      # Get to user_team assigned projects -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      # Example Request: -      #   GET /user_teams/:id/projects -      get ":id/projects" do -        @user_team = UserTeam.find(params[:id]) -        if current_user.admin or current_user.user_teams.include? @user_team -          @projects = paginate @user_team.projects -          present @projects, with: Entities::TeamProject, user_team: @user_team -        else -          not_found! -        end -      end - - -      # Add a new user_team project -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      #   project_id (required) - The ID of a project -      #   greatest_access_level (required) - Project access level -      # Example Request: -      #   POST /user_teams/:id/projects -      post ":id/projects" do -        authenticated_as_admin! -        required_attributes! [:project_id, :greatest_access_level] - -        if not validate_access_level?(params[:greatest_access_level]) -          render_api_error!("Wrong greatest_access_level", 422) -        end - -        @user_team = UserTeam.find(params[:id]) -        if @user_team -          team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id]) - -          # No existing project -          if team_project.nil? -            @user_team.assign_to_projects([params[:project_id]], params[:greatest_access_level]) -            team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id]) -            if team_project.nil? -              render_api_error!("Error creating project assignment", 500) -            else -              @project = team_project.project -              present @project, with: Entities::TeamProject, user_team: @user_team -            end -          else -            render_api_error!("Already exists", 409) -          end -        else -          not_found! -        end -      end - -      # Show a single team project from user_team -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      #   project_id (required) - The ID of a project assigned to the team -      # Example Request: -      #   GET /user_teams/:id/projects/:project_id -      get ":id/projects/:project_id" do -        @user_team = UserTeam.find(params[:id]) -        if current_user.admin or current_user.user_teams.include? @user_team -          team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id]) -          unless team_project.nil? -            present team_project.project, with: Entities::TeamProject, user_team: @user_team -          else -            not_found! -          end -        else -          not_found! -        end -      end - -      # Remove a team project from user_team -      # -      # Parameters: -      #   id (required) - The ID of a user_team -      #   project_id (required) - The ID of a project assigned to the team -      # Example Request: -      #   DELETE /user_teams/:id/projects/:project_id -      delete ":id/projects/:project_id" do -        authenticated_as_admin! - -        @user_team = UserTeam.find(params[:id]) -        if @user_team -          team_project = @user_team.user_team_project_relationships.find_by_project_id(params[:project_id]) -          unless team_project.nil? -            team_project.destroy -          else -            not_found! -          end -        else -          not_found! -        end -      end - -    end -  end -end diff --git a/lib/gitlab/user_team_manager.rb b/lib/gitlab/user_team_manager.rb deleted file mode 100644 index d5ec4ff6676..00000000000 --- a/lib/gitlab/user_team_manager.rb +++ /dev/null @@ -1,146 +0,0 @@ -# UserTeamManager class -# -# Used for manage User teams with project repositories -module Gitlab -  class UserTeamManager -    class << self -      def assign(team, project, access) -        project = Project.find(project) unless project.is_a? Project -        searched_project = team.user_team_project_relationships.find_by_project_id(project.id) - -        unless searched_project.present? -          team.user_team_project_relationships.create(project_id: project.id, greatest_access: access) -          update_team_users_access_in_project(team, project, :added) -        end -      end - -      def resign(team, project) -        project = Project.find(project) unless project.is_a? Project - -        team.user_team_project_relationships.with_project(project).destroy_all - -        update_team_users_access_in_project(team, project, :updated) -      end - -      def update_team_user_membership(team, member, options) -        updates = {} - -        if options[:default_projects_access].present? -          default_projects_access = options[:default_projects_access].to_s - -          if default_projects_access != team.default_projects_access(member).to_s -            updates[:permission] = default_projects_access -          end -        end - -        if options[:group_admin].present? -          group_admin = options[:group_admin].to_s == "1" ? true : false - -          if group_admin != team.admin?(member) -            updates[:group_admin] = group_admin -          end -        end - -        return true if updates.blank? - -        user_team_relationship = team.user_team_user_relationships.find_by_user_id(member) -        return false unless user_team_relationship.update_attributes(updates) - -        rebuild_project_permissions_to_member(team, member) if updates[:permission] - -        true -      end - -      def update_project_greates_access(team, project, permission) -        project_relation = team.user_team_project_relationships.find_by_project_id(project) -        if permission != team.max_project_access(project) -          if project_relation.update_attributes(greatest_access: permission) -            update_team_users_access_in_project(team, project, :updated) -            true -          else -            false -          end -        else -          true -        end -      end - -      def rebuild_project_permissions_to_member(team, member) -        team.projects.each do |project| -          update_team_user_access_in_project(team, member, project, :updated) -        end -      end - -      def update_team_users_access_in_project(team, project, action) -        members = team.members -        members.each do |member| -          update_team_user_access_in_project(team, member, project, action) -        end -      end - -      def update_team_user_access_in_project(team, user, project, action) -        granted_access = max_teams_member_permission_in_project(user, project, action) -        project_team_user = UsersProject.find_by_user_id_and_project_id(user.id, project.id) - -        if granted_access.zero? -          project_team_user.destroy if project_team_user.present? -          return -        end - -        if project_team_user.present? -          project_team_user.update_attributes(project_access: granted_access) -        else -          project.team << [user, granted_access] -        end -      end - -      def max_teams_member_permission_in_project(user, project, action = nil, teams = nil) -        result_access = 0 - -        teams ||= project.user_teams.with_member(user) - -        if action && (action == :added) -          result_access = project.users_projects.with_user(user).first.project_access if project.users_projects.with_user(user).any? -        end - -        if teams.any? -          teams.each do |team| -            granted_access = max_team_member_permission_in_project(team, user, project) -            result_access = [granted_access, result_access].max -          end -        end -        result_access -      end - -      def max_team_member_permission_in_project(team, user, project) -        member_access = team.default_projects_access(user) -        team_access = team.user_team_project_relationships.find_by_project_id(project.id).greatest_access - -        [team_access, member_access].min -      end - -      def add_member_into_team(team, user, access, admin) -        user = User.find(user) unless user.is_a? User - -        team.user_team_user_relationships.create(user_id: user.id, permission: access, group_admin: admin) -        team.projects.each do |project| -          update_team_user_access_in_project(team, user, project, :added) -        end -      end - -      def remove_member_from_team(team, user) -        user = User.find(user) unless user.is_a? User - -        team.user_team_user_relationships.with_user(user).destroy_all -        other_teams = [] -        team.projects.each do |project| -          other_teams << project.user_teams.with_member(user) -        end -        other_teams.uniq -        unless other_teams.any? -          UsersProject.in_projects(team.projects).with_user(user).destroy_all -        end -      end -    end -  end -end  | 
