Class: Fosm::Admin::RolesController

Inherits:
BaseController show all
Defined in:
app/controllers/fosm/admin/roles_controller.rb

Instance Method Summary collapse

Methods inherited from Fosm::ApplicationController

use_host_routes!

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/fosm/admin/roles_controller.rb', line 15

def create
  @assignment = Fosm::RoleAssignment.new(assignment_params)
  @assignment.granted_by_type = fosm_current_user&.class&.name
  @assignment.granted_by_id   = fosm_current_user&.id&.to_s

  if @assignment.save
    Fosm::AccessEventJob.perform_later({
      "action"             => "grant",
      "user_type"          => @assignment.user_type,
      "user_id"            => @assignment.user_id,
      "user_label"         => resolve_user_label(@assignment.user_type, @assignment.user_id),
      "resource_type"      => @assignment.resource_type,
      "resource_id"        => @assignment.resource_id,
      "role_name"          => @assignment.role_name,
      "performed_by_type"  => fosm_current_user&.class&.name,
      "performed_by_id"    => fosm_current_user&.id&.to_s,
      "performed_by_label" => (fosm_current_user.respond_to?(:email) ? fosm_current_user.email : fosm_current_user.to_s)
    })

    # Invalidate the per-request RBAC cache for the affected user so their
    # new role is visible immediately in the same request (unlikely but correct)
    user = @assignment.actor
    Fosm::Current.invalidate_for(user) if user

    redirect_to fosm.admin_roles_path, notice: "Role :#{@assignment.role_name} granted to #{@assignment.actor_label}."
  else
    @apps = Fosm::Registry.all
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/fosm/admin/roles_controller.rb', line 85

def destroy
  @assignment = Fosm::RoleAssignment.find(params[:id])

  Fosm::AccessEventJob.perform_later({
    "action"             => "revoke",
    "user_type"          => @assignment.user_type,
    "user_id"            => @assignment.user_id,
    "user_label"         => @assignment.actor_label,
    "resource_type"      => @assignment.resource_type,
    "resource_id"        => @assignment.resource_id,
    "role_name"          => @assignment.role_name,
    "performed_by_type"  => fosm_current_user&.class&.name,
    "performed_by_id"    => fosm_current_user&.id&.to_s,
    "performed_by_label" => (fosm_current_user.respond_to?(:email) ? fosm_current_user.email : fosm_current_user.to_s)
  })

  user = @assignment.actor
  @assignment.destroy!
  Fosm::Current.invalidate_for(user) if user

  redirect_to fosm.admin_roles_path, notice: "Role revoked."
end

#indexObject



4
5
6
7
8
# File 'app/controllers/fosm/admin/roles_controller.rb', line 4

def index
  @assignments     = Fosm::RoleAssignment.order(created_at: :desc)
  @access_events   = Fosm::AccessEvent.recent.limit(20)
  @apps            = Fosm::Registry.all
end

#newObject



10
11
12
13
# File 'app/controllers/fosm/admin/roles_controller.rb', line 10

def new
  @assignment = Fosm::RoleAssignment.new
  @apps       = Fosm::Registry.all
end

#refresh_registryObject



46
47
48
49
50
51
52
# File 'app/controllers/fosm/admin/roles_controller.rb', line 46

def refresh_registry
  Fosm::Registry.clear!
  Fosm::Registry.repopulate!
  count = Fosm::Registry.all.size
  redirect_to fosm.admin_roles_path,
    notice: "Registry refreshed — #{count} model#{count == 1 ? '' : 's'} registered."
end

#users_searchObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/fosm/admin/roles_controller.rb', line 54

def users_search
  q         = params[:q].to_s.strip
  user_type = params[:user_type].presence || "User"
  results   = []

  begin
    klass = user_type.constantize
    scope = klass.all

    if q.present?
      searchable = klass.column_names & %w[email name]
      if searchable.any?
        conditions = searchable.map { |col| "lower(#{col}) LIKE :q" }.join(" OR ")
        scope = scope.where(conditions, q: "%#{q.downcase}%")
      end
    end

    results = scope.limit(10).map do |user|
      label = [
        (user.name if user.respond_to?(:name) && user.name.present?),
        (user.email if user.respond_to?(:email))
      ].compact.join("")
      { id: user.id.to_s, label: label }
    end
  rescue NameError
    # unknown user_type — return empty
  end

  render json: results
end