mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
What people can do on an aircraft now comes from their role there, and administrators set what each role allows (Admin → Roles). - Roles are installation-wide sets of permissions (31, grouped as flying, squawks and notes, maintenance records, imports and exports, the aircraft, notifications). Seeing an aircraft's records needs none. - Built in: Owner (always every permission, so an aircraft can always be managed), Mechanic (shop maintenance: logbook, signing, components, inspections, ADs, squawks, reminders), Pilot (what pilots could do before) and Viewer (view only). Administrators can change all but Owner's, and add their own; a role can be deleted once nobody has it. - Existing owners and pilots keep exactly what they could do. - Admin → Users → a user lists their role on every aircraft, to change, remove or add. On the aircraft's Access page, anyone who manages people hands out roles, but only an Owner can give or take away Owner, nobody changes their own (except administrators), and an aircraft always keeps an Owner (AircraftAccess). - Controllers declare the permission each action needs; a request that changes something with none declared is refused (and raises in development and tests), and a test walks every route to check. - Weekly reminders and overdue alerts go to roles that get reminders; the DRS reminder also needs Manage ADs. The API follows the person's role; its owners_only error is now not_permitted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
2.3 KiB
Ruby
53 lines
2.3 KiB
Ruby
# Giving, changing and taking away people's roles on an aircraft, by someone
|
|
# allowed to (the aircraft's "manage people", or an administrator). The rules,
|
|
# the same from the aircraft's Access page and Admin → Users:
|
|
# - only an Owner or an administrator can give or take away the Owner role
|
|
# (otherwise anyone who manages people could make themselves Owner);
|
|
# - only an administrator can change or remove their own role;
|
|
# - an aircraft always keeps at least one Owner.
|
|
# Each method returns nil when done, or a message saying why not.
|
|
class AircraftAccess
|
|
def initialize(aircraft, actor:)
|
|
@aircraft = aircraft
|
|
@actor = actor
|
|
end
|
|
|
|
def add(user, role)
|
|
return t("not_found") unless user && role
|
|
return t("owner_only") if role.owner? && !actor_owner?
|
|
membership = @aircraft.roles.new(user: user, role: role)
|
|
return membership.errors.full_messages.to_sentence unless membership.save
|
|
@aircraft.log_event!("role", I18n.t("flash.aircraft_roles.added_as", display_name: user.display_name, role: role.name), user: @actor)
|
|
nil
|
|
end
|
|
|
|
def change(membership, role)
|
|
return t("not_found") unless role
|
|
return if membership.role == role
|
|
return t("own_role") if membership.user == @actor && !@actor.admin?
|
|
return t("owner_only") if (role.owner? || membership.owner?) && !actor_owner?
|
|
return t("last_owner") if membership.owner? && !role.owner? && last_owner?
|
|
membership.update!(role: role)
|
|
@aircraft.log_event!("role", I18n.t("flash.aircraft_roles.changed_to", display_name: membership.user.display_name, role: role.name), user: @actor)
|
|
nil
|
|
end
|
|
|
|
def remove(membership)
|
|
return t("own_role") if membership.user == @actor && !@actor.admin?
|
|
return t("owner_only") if membership.owner? && !actor_owner?
|
|
return t("last_owner") if membership.owner? && last_owner?
|
|
membership.destroy!
|
|
@aircraft.log_event!("role", I18n.t("flash.aircraft_roles.removed", display_name: membership.user.display_name), user: @actor)
|
|
nil
|
|
end
|
|
|
|
# The roles this person can hand out here (Owner only by an Owner).
|
|
def assignable_roles = Role.ordered.select { |role| !role.owner? || actor_owner? }
|
|
|
|
private
|
|
|
|
def actor_owner? = @actor.admin? || @actor.role_for(@aircraft)&.owner?
|
|
def last_owner? = @aircraft.roles.owners.count <= 1
|
|
def t(key) = I18n.t("services.aircraft_access.#{key}")
|
|
end
|