Sha256: 75ebd2f5e66806fed682827bb22cad3767569cd2328646460c016009c4b57fb6

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 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

16 entries across 16 versions & 1 rubygems

Version Path
blacklight-spotlight-4.1.2 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.1.1 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.1.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.0.3 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.0.2 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.0.1 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-4.0.0 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta10 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta9 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta8 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta7 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta6 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta5 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta4 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta3 app/controllers/spotlight/admin_users_controller.rb
blacklight-spotlight-3.6.0.beta1 app/controllers/spotlight/admin_users_controller.rb