Sha256: 8c1d656e7947fff6e4d4b6367bf1f3ba7edeefe046077875fcd3341118f4041c

Contents?: true

Size: 1.74 KB

Versions: 13

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # A controller to handle the adminstration of site admin users
  class AdminUsersController < Spotlight::ApplicationController
    before_action :authenticate_user!
    before_action :load_site
    before_action :load_users

    load_and_authorize_resource :site, class: 'Spotlight::Site'

    def index
      add_breadcrumb(t(:'spotlight.sites.home'), root_url)
      add_breadcrumb(t(:'spotlight.admin_users.index.page_title'))
    end

    def create
      if update_roles
        Spotlight::InviteUsersService.call(resource: @site)
        flash[:notice] = t('.success')
      else
        flash[:error] = t('.error')
      end

      redirect_to spotlight.admin_users_path
    end

    def update
      user = Spotlight::Engine.user_class.find(params[:id])
      if user
        Spotlight::Role.create(user_key: user.email, role: 'admin', resource: @site).save
        flash[:notice] = t('spotlight.admin_users.create.success')
      else
        flash[:error] = t('spotlight.admin_users.create.error')
      end

      redirect_to spotlight.admin_users_path
    end

    def destroy
      user = Spotlight::Engine.user_class.find(params[:id])
      if user.roles.where(resource: @site).first.destroy
        flash[:notice] = t('.success')
      else
        flash[:error] = t('.error')
      end
      redirect_to spotlight.admin_users_path
    end

    private

    def load_users
      @users ||= ::User.all.reject(&:guest?)
    end

    def load_site
      @site ||= Spotlight::Site.instance
    end

    def create_params
      params.require(:user).permit(:email)
    end

    def update_roles
      Spotlight::Role.create(user_key: create_params[:email], role: 'admin', resource: @site).save
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
blacklight-spotlight-4.7.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.6.1 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.6.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.5.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.4.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.6 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.5 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.4 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.3 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.2 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.1 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.3.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.2.0 app/controllers/spotlight/admin_users_controller.rb