Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/controllers/admin/oauth_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,13 @@ def set_application
@application = Doorkeeper::Application.find_by_public_param!(params[:id])
end

# Only a superadmin links an app to a patch: introspection names the patch,
# and a resource server such as Quilt may trust it to tell which patch calls.
def application_params
params.require(:doorkeeper_application).permit(:name, :redirect_uri, :scopes, :confidential,
:show_in_launcher, :launch_url, :launcher_icon_url, :launcher_description)
permitted = [:name, :redirect_uri, :scopes, :confidential,
:show_in_launcher, :launch_url, :launcher_icon_url, :launcher_description]
permitted << :patch_id if current_user.superadmin?
params.require(:doorkeeper_application).permit(*permitted)
end

end
66 changes: 66 additions & 0 deletions app/controllers/admin/patch_role_assignments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

# Gives a role of a patch to a user or a group, or takes it away (see
# PatchRoleAssignment). Only a superadmin gives a role, like the roles
# themselves. Any admin takes one away, so access is quick to remove.
class Admin::PatchRoleAssignmentsController < Admin::BaseController
before_action :set_role
before_action :require_superadmin, only: [:create]

def create
assignee = find_assignee
return redirect_back_to_roles(alert: assignee_not_found_message) unless assignee

assignment = @role.assignments.new(assignee: assignee, created_by: current_user)

if assignment.save
redirect_back_to_roles(notice: "#{assignee_label(assignee)} has the #{@role.name} role now.")
else
redirect_back_to_roles(alert: assignment.errors.full_messages.to_sentence)
end
end

def destroy
assignment = @role.assignments.find_by_public_param!(params[:id])
assignment.destroy!
redirect_back_to_roles(notice: "#{assignee_label(assignment.assignee)} no longer has the #{@role.name} role.")
end

private

def set_role
@patch = Patch.find_by!(slug: params[:patch_id])
@role = @patch.roles.find_by_public_param!(params[:role_id])
end

def require_superadmin
return if current_user.superadmin?

redirect_to admin_patch_path(@patch), alert: "Only a superadmin can change the roles of a patch."
end

def assignment_params
params.require(:assignment).permit(:group_id, :email)
end

def find_assignee
if assignment_params[:group_id].present?
Group.find_by_public_id(assignment_params[:group_id].to_s)
elsif assignment_params[:email].present?
User.find_for_any_email(assignment_params[:email])
end
end

def assignee_not_found_message
assignment_params[:group_id].present? ? "That group doesn't exist." : "No user has that email address."
end

def assignee_label(assignee)
assignee.is_a?(Group) ? "The #{assignee.name} group" : assignee.full_name
end

def redirect_back_to_roles(**flash)
redirect_to admin_patch_path(@patch, anchor: "roles"), **flash
end

end
59 changes: 59 additions & 0 deletions app/controllers/admin/patch_roles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

# Adds a role to a patch, renames it or deletes it (see PatchRole).
# Superadmin only: apps such as Quilt read the role from the `patches` claim,
# so a role can open a patch's finance data.
class Admin::PatchRolesController < Admin::BaseController
before_action :set_patch
before_action :require_superadmin
before_action :set_role, only: [:update, :destroy]

def create
role = @patch.roles.new(role_params.merge(created_by: current_user))

if role.save
redirect_back_to_roles(notice: "The #{role.name} role was added.")
else
redirect_back_to_roles(alert: role.errors.full_messages.to_sentence)
end
end

# Changes the name and the description. The key never changes.
def update
if @role.update(role_params.except(:key))
redirect_back_to_roles(notice: "The #{@role.name} role was updated.")
else
redirect_back_to_roles(alert: @role.errors.full_messages.to_sentence)
end
end

def destroy
@role.destroy!
redirect_back_to_roles(notice: "The #{@role.name} role was deleted.")
end

private

def set_patch
@patch = Patch.find_by!(slug: params[:patch_id])
end

def set_role
@role = @patch.roles.find_by_public_param!(params[:id])
end

def require_superadmin
return if current_user.superadmin?

redirect_to admin_patch_path(@patch), alert: "Only a superadmin can change the roles of a patch."
end

def role_params
params.require(:role).permit(:key, :name, :description)
end

def redirect_back_to_roles(**flash)
redirect_to admin_patch_path(@patch, anchor: "roles"), **flash
end

end
72 changes: 72 additions & 0 deletions app/controllers/admin/patches_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

# Patches in the admin panel (see Patch).
#
# Any admin may look at patches. Only a superadmin may create a patch or
# change its name and description, like groups. The slug never changes, and
# there is no delete: role assignments, Quilt and app tokens name the slug.
#
# The show page lists the patch leads, the patch's own roles with who holds
# them, and the apps linked to the patch. A patch lead changes through
# identity governance. The patch's own roles change through
# Admin::PatchRolesController and Admin::PatchRoleAssignmentsController
# (superadmin only).
class Admin::PatchesController < Admin::BaseController
before_action :set_patch, only: [:show, :edit, :update]
before_action :require_superadmin, except: [:index, :show]

def index
@patches = Patch.order(:name)
@application_counts = Doorkeeper::Application.where.not(patch_id: nil).group(:patch_id).count
end

def show
@applications = @patch.applications.order(:name)
@leads = RoleGrants.assignments("patch_lead").where(scope: @patch.slug).includes(:granted_by).to_a
@roles = @patch.roles.includes(assignments: :assignee).order(:key)
# Only a manual group can hold a patch role (see PatchRoleAssignment).
@groups = Group.manual.order(:name) if current_user.superadmin?
end

def new
@patch = Patch.new
end

def create
@patch = Patch.new(patch_params.merge(created_by: current_user))

if @patch.save
redirect_to admin_patch_path(@patch), notice: "The patch was created."
else
render :new, status: :unprocessable_content
end
end

def edit
end

def update
if @patch.update(patch_params.except(:slug))
redirect_to admin_patch_path(@patch), notice: "The patch was updated."
else
render :edit, status: :unprocessable_content
end
end

private

def set_patch
@patch = Patch.find_by!(slug: params[:id])
end

def patch_params
params.require(:patch).permit(:name, :slug, :description)
end

def require_superadmin
return if current_user.superadmin?

redirect_to admin_patches_path, alert: "Only a superadmin can change patches."
end

end
47 changes: 44 additions & 3 deletions app/controllers/api/v1/directory/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# GET /api/v1/directory/users/:sub
# GET /api/v1/directory/users?role=<key> or ?group=<slug>
# GET /api/v1/directory/users?patch=<slug>&patch_role=<key> (key "lead" for
# the patch leads). Only for an app whose scopes list `patches`, and only
# for the patch the app belongs to (see PatchRoles).
#
# A user the calling app may not serve is reported as not found, the same as
# an unknown user, so the app can't learn who else has a Weave account.
Expand All @@ -14,6 +17,10 @@ def index
return if performed?

users = users.select { |user| AppAccess.permitted?(user, current_application) }
# The patches field reads each user's roles (see PatchRoles).
if current_application.scopes.exists?("patches")
ActiveRecord::Associations::Preloader.new(records: users, associations: :active_user_roles).call
end
render json: { users: users.sort_by(&:p_id).map { |user| serialize(user) } }
end

Expand All @@ -27,12 +34,16 @@ def show
private

def filtered_users
if params[:role].present? == params[:group].present?
render json: { error: "invalid_request", error_description: "Pass exactly one of role or group" }, status: :bad_request
filters = [params[:role], params[:group], params[:patch]].count(&:present?)
if filters != 1 || params[:patch].present? != params[:patch_role].present?
render json: { error: "invalid_request", error_description: "Pass exactly one of role, group or patch (with patch_role)" },
status: :bad_request
elsif params[:role].present?
users_with_role
else
elsif params[:group].present?
users_in_group
else
users_with_patch_role
end
end

Expand All @@ -54,6 +65,36 @@ def users_in_group
group.users.to_a
end

# The holders of one patch role, directly or through an active group
# membership (PatchRole.held_by), or the patch leads for "lead".
# Needs the patches scope on the app, like the claim. A patch other
# than the app's own, and an unknown role key, are not found.
def users_with_patch_role
unless current_application.scopes.exists?("patches")
return render json: { error: "insufficient_scope", error_description: "Use an app allowed the patches scope" },
status: :forbidden
end

patch = current_application.patch
return render_not_found unless patch && patch.slug == params[:patch]

patch_role_holders(patch, params[:patch_role].to_s)
end

def patch_role_holders(patch, key)
if key == PatchRoles::LEAD
return User.where(id: UserRole.active.for_role("patch_lead").where(scope: patch.slug).select(:user_id)).to_a
end

role = patch.roles.find_by(key:)
return render_not_found unless role

assignments = role.assignments.to_a
direct = User.where(id: assignments.select { |a| a.assignee_type == "User" }.map(&:assignee_id))
via_group = User.where(id: Group::Membership.active.where(group_id: assignments.select { |a| a.assignee_type == "Group" }.map(&:assignee_id)).select(:user_id))
direct.or(via_group).to_a
end

def serialize(user) = DirectoryUserSerializer.new(user, current_application).as_json

end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/admin_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def admin_nav_groups(user = current_user)
%w[role_assignments], :admin)
end),
(admin_nav_entry("Groups", admin_groups_path, :users_round, "admin teams", %w[groups group_memberships], :admin) if admin),
(admin_nav_entry("Patches", admin_patches_path, :rocket, "admin grant programs ganymede quilt", %w[patches patch_roles patch_role_assignments], :admin) if admin),
(if admin
admin_nav_entry("Consents", admin_guardian_consents_path, :shield_check, "admin guardian minors review queue",
%w[guardian_consents], :admin)
Expand Down
19 changes: 19 additions & 0 deletions app/models/concerns/oauth_application_patch_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# The patch an OAuth app belongs to, if any (see Patch). Included into
# Doorkeeper::Application by config/initializers/oauth_application_public_id.rb.
#
# Introspection of the app's client_credentials tokens names the patch slug as
# `patch` (see config/initializers/doorkeeper.rb), so a resource server such as
# Quilt can tell which patch the calling app belongs to.
module OauthApplicationPatchLink
extend ActiveSupport::Concern

included do
belongs_to :patch, optional: true, inverse_of: :applications

# A patch_id that names no patch is a form error, not a foreign key error.
validates :patch, presence: { message: "must be a patch that exists" }, if: :patch_id?
end

end
6 changes: 6 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Group < ApplicationRecord
has_many :access_requests, dependent: nil
has_many :application_access_grants, as: :grantee, dependent: :destroy
has_many :application_role_assignments, as: :assignee, dependent: :destroy
has_many :patch_role_assignments, as: :assignee, dependent: :destroy

before_validation :generate_slug, on: :create

Expand All @@ -90,6 +91,11 @@ def editable? = manual?

def managed_by?(user) = user.present? && manual? && manager_memberships.exists?(user_id: user.id)

# A group that holds a patch role gives that role to every member, and a
# patch role can open finance data in Quilt. So only a superadmin adds a
# member to it (see Group::Membership and GroupPolicy#add_member?).
def holds_patch_roles? = patch_role_assignments.exists?

private

def generate_slug
Expand Down
17 changes: 17 additions & 0 deletions app/models/group/membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Group::Membership < ApplicationRecord
validates :user_id, uniqueness: { scope: :group_id, message: "is already in this group" }
validate :expires_in_the_future, if: -> { expires_at.present? && will_save_change_to_expires_at? }
validate :manager_is_manual, if: :role_manager?
validate :patch_role_group_added_by_superadmin, if: :joins_group?

def expired? = expires_at.present? && expires_at <= Time.current

Expand Down Expand Up @@ -102,6 +103,22 @@ def expires_in_the_future
errors.add(:expires_at, "must be in the future") if expires_at <= Time.current
end

# A new row, or an expired row that starts again.
def joins_group?
return true if new_record?

will_save_change_to_expires_at? && expires_at_in_database.present? && expires_at_in_database <= Time.current
end

# Every path that adds a member (a group manager, an approved access
# request, the admin panel, a rake task) passes here.
def patch_role_group_added_by_superadmin
return unless group&.holds_patch_roles?
return if added_by&.superadmin?

errors.add(:base, "Only a superadmin can add a member to #{group.name}, because the group holds patch roles")
end

def manager_is_manual
errors.add(:role, "can be manager only in a manual group") unless manual? && group&.manual?
end
Expand Down
Loading
Loading