From b7ac2705dfe667c052adeda35d1f52ffd3f41c82 Mon Sep 17 00:00:00 2001 From: 839 <8398a7@gmail.com> Date: Fri, 1 Nov 2024 21:22:28 +0900 Subject: [PATCH] Role management with rolify --- .../admin/dashboards_controller.rb | 2 +- app/controllers/admin/sheets_controller.rb | 2 +- app/controllers/api/api_controller.rb | 4 +-- app/controllers/api/v1/messages_controller.rb | 2 +- app/controllers/application_controller.rb | 21 ----------- app/controllers/logs_controller.rb | 2 +- app/helpers/application_helper.rb | 6 +--- app/javascript/app/Adsense/Adsenses.tsx | 30 ---------------- app/javascript/app/Adsense/index.tsx | 18 ---------- .../app/Sheet/components/SheetList/index.tsx | 2 -- .../app/Welcome/components/index.tsx | 2 -- app/javascript/lib/models/Environment.ts | 4 --- app/models/concerns/user/api.rb | 1 - app/models/concerns/user/role.rb | 29 --------------- app/models/role.rb | 31 ++++++++++++++++ app/models/user.rb | 11 ++++-- app/views/layouts/_footer.html.slim | 2 +- app/views/layouts/_nav.html.slim | 4 +-- app/views/layouts/application.html.slim | 3 +- app/views/logs/list.html.slim | 3 -- app/views/logs/sheet.html.slim | 3 -- app/views/logs/show.html.slim | 8 ++--- app/views/recommends/index.html.slim | 3 -- app/views/sheets/power.html.slim | 3 -- config/initializers/rolify.rb | 10 ++++++ .../20241030141643_rolify_create_roles.rb | 18 ++++++++++ .../20241030141745_remove_role_from_users.rb | 5 +++ db/schema.rb | 21 +++++++++-- docs/erd.png | Bin 356848 -> 388720 bytes spec/factories/users.rb | 4 +-- spec/models/role_spec.rb | 22 ++++++++++++ spec/models/user_spec.rb | 1 - spec/requests/users_spec.rb | 1 - spec/systems/admin/dashboards_spec.rb | 5 +-- spec/systems/admin/messages_spec.rb | 21 ++++++----- spec/systems/admin/rails_admin_spec.rb | 2 +- spec/systems/admin/users_spec.rb | 33 +++++++++--------- spec/systems/logs/show_logs_spec.rb | 10 +++--- spec/systems/navbar/display_navbar_spec.rb | 9 ++--- 39 files changed, 172 insertions(+), 186 deletions(-) delete mode 100644 app/javascript/app/Adsense/Adsenses.tsx delete mode 100644 app/javascript/app/Adsense/index.tsx delete mode 100644 app/models/concerns/user/role.rb create mode 100644 app/models/role.rb create mode 100644 config/initializers/rolify.rb create mode 100644 db/migrate/20241030141643_rolify_create_roles.rb create mode 100644 db/migrate/20241030141745_remove_role_from_users.rb create mode 100644 spec/models/role_spec.rb diff --git a/app/controllers/admin/dashboards_controller.rb b/app/controllers/admin/dashboards_controller.rb index 27d0a071..67deebe9 100644 --- a/app/controllers/admin/dashboards_controller.rb +++ b/app/controllers/admin/dashboards_controller.rb @@ -2,7 +2,7 @@ class Admin::DashboardsController < ApplicationController before_action :authenticate_user! - before_action :owner_user! + before_action :admin_user! def index @email = User.where.not(email: '').count diff --git a/app/controllers/admin/sheets_controller.rb b/app/controllers/admin/sheets_controller.rb index 16243ca8..157287d6 100644 --- a/app/controllers/admin/sheets_controller.rb +++ b/app/controllers/admin/sheets_controller.rb @@ -2,7 +2,7 @@ class Admin::SheetsController < ApplicationController before_action :authenticate_user! - before_action :member_user! + before_action :admin_user! before_action :load_sheet, except: %i[index new create] def index diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 2ec4fcbb..937d8f09 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -20,9 +20,9 @@ def authenticate! raise UnauthorizedError unless current_user end - def authenticate_member! + def authenticate_admin! raise UnauthorizedError unless current_user - raise UnauthorizedError if current_user.role < User::Role::MEMBER + raise UnauthorizedError unless current_user.admin? end def authenticate_slack! diff --git a/app/controllers/api/v1/messages_controller.rb b/app/controllers/api/v1/messages_controller.rb index cd1b578b..95148b99 100644 --- a/app/controllers/api/v1/messages_controller.rb +++ b/app/controllers/api/v1/messages_controller.rb @@ -2,7 +2,7 @@ class Api::V1::MessagesController < Api::V1::BaseController def index - authenticate_member! + authenticate_admin! render json: { num: Message.where(state: false).count } end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 92ef7f9c..5c777915 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -45,27 +45,6 @@ def admin_user! redirect_to root_path end - def special_user! - return if current_user.special? - - flash[:danger] = '不正な操作です.' - redirect_to list_log_path - end - - def owner_user! - return if current_user.owner? - - flash[:danger] = '許可されていないページです' - redirect_to root_path - end - - def member_user! - return if current_user.member? - - flash[:danger] = '許可されていないページです' - redirect_to root_path - end - def handle_unverified_request super rescue ActionController::InvalidAuthenticityToken => e diff --git a/app/controllers/logs_controller.rb b/app/controllers/logs_controller.rb index 1483fdc0..e5e15618 100644 --- a/app/controllers/logs_controller.rb +++ b/app/controllers/logs_controller.rb @@ -54,7 +54,7 @@ def show end def destroy - log = current_user.owner? ? Log.find(params[:id]) : current_user.logs.find(params[:id]) + log = current_user.admin? ? Log.find(params[:id]) : current_user.logs.find(params[:id]) if log flash[:notice] = "#{log.title}のログを削除し,状態を戻しました" log.destroy diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2726c347..85be1b46 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -5,10 +5,6 @@ def return_ability_rival(cnt) params[:action] == 'clear' ? @sheets[cnt].n_ability : @sheets[cnt].h_ability end - def adsense(slot = 2) - react_component_wrapper 'Adsense', slot: slot - end - def react_component_wrapper(component, props = {}) props[:context] = { sentry_dsn: ENV['SENTRY_JS_DSN'] @@ -19,7 +15,7 @@ def react_component_wrapper(component, props = {}) def render_ads? return true unless current_user - !(current_user.special? || current_user.owner?) + !(current_user.admin? || current_user.skip_ad?) end def recent_link(iidxid) diff --git a/app/javascript/app/Adsense/Adsenses.tsx b/app/javascript/app/Adsense/Adsenses.tsx deleted file mode 100644 index 029680c3..00000000 --- a/app/javascript/app/Adsense/Adsenses.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import React from 'react'; -// @ts-ignore -import Adsense from 'react-adsense'; -import { useSelector } from 'react-redux'; - -import { RootState } from '../../lib/ducks'; - -const Adsenses: React.SFC<{ slot: 1 | 2 }> = (props) => { - const { client, slots } = useSelector( - (state: RootState) => state.$$meta.env.adsense, - ); - const $$currentUser = useSelector( - (state: RootState) => state.$$meta.currentUser, - ); - - const slot = slots[props.slot - 1]; - if ($$currentUser && !$$currentUser.renderAds()) { - return null; - } - return ( -
- -
- ); -}; - -export default Adsenses; diff --git a/app/javascript/app/Adsense/index.tsx b/app/javascript/app/Adsense/index.tsx deleted file mode 100644 index 9046477c..00000000 --- a/app/javascript/app/Adsense/index.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import React from 'react'; -import { Provider } from 'react-redux'; -import rootReducer, { rootSaga, RootState } from '../../lib/ducks'; -import { actions, initialState } from '../../lib/ducks/Meta'; -import storeCreator from '../../lib/store'; -import Adsenses from './Adsenses'; - -export default (props: { slot: 1 | 2 } & AbilitysheetContext) => { - const store = storeCreator(props, rootReducer, rootSaga, { - $$meta: initialState, - }); - store.dispatch(actions.considerQueryString()); - return ( - - - - ); -}; diff --git a/app/javascript/app/Sheet/components/SheetList/index.tsx b/app/javascript/app/Sheet/components/SheetList/index.tsx index 1de30cc9..4136055f 100644 --- a/app/javascript/app/Sheet/components/SheetList/index.tsx +++ b/app/javascript/app/Sheet/components/SheetList/index.tsx @@ -1,6 +1,5 @@ import React, { SFC, useCallback } from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import Adsenses from '../../../Adsense/Adsenses'; import { RootState } from '../../ducks'; import { actions } from '../../ducks/Sheet'; import LampTd from './LampTd'; @@ -143,7 +142,6 @@ const SheetList: SFC = (props) => { return ( <> - {owner() ? (