mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Configurable roles: Owner, Mechanic, Pilot, Viewer and custom roles
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>
This commit is contained in:
1 parent
d2cd60c6a7
commit
d8cd462194
101 files changed
+1032
-232
No files matched your search
@@ -0,0 +1,57 @@
|
||||
# The roles people can have on an aircraft and what each allows (Role,
|
||||
# Permission). Owner always has every permission; the other built-in roles
|
||||
# can be changed but not deleted; administrators can add their own.
|
||||
class Admin::RolesController < ApplicationController
|
||||
before_action :require_admin!
|
||||
before_action :set_role, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
Role.ensure_built_in!
|
||||
@roles = Role.ordered
|
||||
@counts = AircraftRole.group(:role_id).count
|
||||
end
|
||||
|
||||
def new
|
||||
@role = Role.new(permissions: Permission::DEFAULTS["viewer"])
|
||||
end
|
||||
|
||||
def create
|
||||
@role = Role.new(role_params)
|
||||
if @role.save
|
||||
redirect_to admin_roles_path, notice: t("flash.admin_roles.created", name: @role.name)
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
attributes = @role.owner? ? role_params.except(:permissions) : role_params
|
||||
if @role.update(attributes)
|
||||
redirect_to admin_roles_path, notice: t("flash.admin_roles.updated", name: @role.name)
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @role.built_in?
|
||||
redirect_to admin_roles_path, alert: t("flash.admin_roles.built_in")
|
||||
elsif @role.aircraft_roles.exists?
|
||||
redirect_to admin_roles_path, alert: t("flash.admin_roles.in_use", name: @role.name, count: @role.aircraft_roles.count)
|
||||
else
|
||||
@role.destroy!
|
||||
redirect_to admin_roles_path, notice: t("flash.admin_roles.deleted", name: @role.name)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_role = @role = Role.find(params[:id])
|
||||
|
||||
# Unticking every box sends none, so an empty list is always sent too.
|
||||
def role_params
|
||||
params.require(:role).permit(:name, :description, permissions: []).tap { _1[:permissions] = Array(_1[:permissions]).compact_blank }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
# Administrators give people roles on any aircraft (from Admin → Users),
|
||||
# under the same rules as the aircraft's Access page (AircraftAccess).
|
||||
class Admin::UserAircraftRolesController < ApplicationController
|
||||
before_action :require_admin!
|
||||
before_action :set_user
|
||||
|
||||
def create
|
||||
aircraft = Aircraft.find_by(id: params[:aircraft_id])
|
||||
return back(alert: t("services.aircraft_access.not_found")) unless aircraft
|
||||
back_with AircraftAccess.new(aircraft, actor: current_user).add(@user, Role.find_by(id: params[:role_id])),
|
||||
t("flash.admin_users.access_added", tail_number: aircraft.tail_number)
|
||||
end
|
||||
|
||||
def update
|
||||
membership = @user.aircraft_roles.find(params[:id])
|
||||
back_with AircraftAccess.new(membership.aircraft, actor: current_user).change(membership, Role.find_by(id: params[:role_id])),
|
||||
t("flash.admin_users.access_changed", tail_number: membership.aircraft.tail_number)
|
||||
end
|
||||
|
||||
def destroy
|
||||
membership = @user.aircraft_roles.find(params[:id])
|
||||
back_with AircraftAccess.new(membership.aircraft, actor: current_user).remove(membership),
|
||||
t("flash.admin_users.access_removed", tail_number: membership.aircraft.tail_number)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_user = @user = User.find(params[:user_id])
|
||||
def back(flash) = redirect_to(edit_admin_user_path(@user, anchor: "access"), flash)
|
||||
def back_with(error, notice) = back(error ? { alert: error } : { notice: notice })
|
||||
end
|
||||
@@ -3,7 +3,7 @@ class Admin::UsersController < ApplicationController
|
||||
before_action :set_user, only: %i[edit update destroy disable enable unlock reset_two_factor]
|
||||
|
||||
def index
|
||||
@users = User.order(:email_address).includes(:certificates, aircraft_roles: :aircraft)
|
||||
@users = User.order(:email_address).includes(:certificates, aircraft_roles: %i[aircraft role])
|
||||
end
|
||||
|
||||
def new
|
||||
|
||||
Reference in new issue
Block a user