Skip to content

Commit

Permalink
Role management with rolify
Browse files Browse the repository at this point in the history
  • Loading branch information
8398a7 committed Nov 1, 2024
1 parent 1bf69af commit b7ac270
Show file tree
Hide file tree
Showing 39 changed files with 172 additions and 186 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/dashboards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/sheets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 0 additions & 21 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/logs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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)
Expand Down
30 changes: 0 additions & 30 deletions app/javascript/app/Adsense/Adsenses.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions app/javascript/app/Adsense/index.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions app/javascript/app/Sheet/components/SheetList/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -143,7 +142,6 @@ const SheetList: SFC = (props) => {

return (
<>
<Adsenses slot={1} />
{owner() ? (
<button
onClick={handleToggleDisplaySelect}
Expand Down
2 changes: 0 additions & 2 deletions app/javascript/app/Welcome/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { useSelector } from 'react-redux';
import SplineGraph from '../../../lib/components/SplineGraph';
import { RootState } from '../../../lib/ducks';
import Adsenses from '../../Adsense/Adsenses';
import TopPanel from './TopPanel';
import TwitterContents from './TwitterContents';

Expand All @@ -14,7 +13,6 @@ const Welcome: React.SFC = () => {
return (
<>
<TopPanel {...{ user, mobile }} />
<Adsenses slot={1} />
<div>
SP ☆12以外を管理したい人向け:{' '}
<a
Expand Down
4 changes: 0 additions & 4 deletions app/javascript/lib/models/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ const defaultValue = {
],
implicitMobile: implicitMobile(),
explicitDesktop: false,
adsense: {
client: 'ca-pub-5751776715932993',
slots: ['3154093489', '6067784017'],
},
};

export default class Environment extends Record(defaultValue) {
Expand Down
1 change: 0 additions & 1 deletion app/models/concerns/user/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def schema
{
id: id,
iidxid: iidxid,
role: role,
djname: djname,
grade: grade,
pref: pref,
Expand Down
29 changes: 0 additions & 29 deletions app/models/concerns/user/role.rb

This file was deleted.

31 changes: 31 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: roles
#
# id :bigint(8) not null, primary key
# name :string
# resource_type :string
# resource_id :bigint(8)
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_roles_on_name_and_resource_type_and_resource_id (name,resource_type,resource_id)
# index_roles_on_resource (resource_type,resource_id)
#
class Role < ApplicationRecord
has_and_belongs_to_many :users, join_table: :users_roles

belongs_to :resource,
polymorphic: true,
optional: true

validates :resource_type,
inclusion: { in: Rolify.resource_types },
allow_nil: true

scopify
end
11 changes: 9 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# djname :string not null
# grade :integer
# pref :integer not null
# role :integer default(0), not null
# failed_attempts :integer default(0), not null
# unlock_token :string
# locked_at :datetime
Expand All @@ -37,6 +36,7 @@
#

class User < ApplicationRecord
rolify
devise :database_authenticatable, :registerable, :omniauthable, :rememberable, :recoverable, :trackable, :validatable, :lockable
attr_accessor :login

Expand All @@ -46,7 +46,6 @@ class User < ApplicationRecord
include User::DeviseMethods
include User::FollowMethods
include User::List
include User::Role
include User::Static
include User::Ist

Expand Down Expand Up @@ -102,6 +101,14 @@ def pref_name
User::Static::PREF[pref]
end

def admin?
has_role?(:admin)
end

def ad_skip?
admin? || has_role?(:ad_skip)
end

class << self
def find_for_oauth(auth, user)
social = Social.find_by(uid: auth.uid, provider: auth.provider)
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_footer.html.slim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
footer.footer
.content.has-text-centered
p &copy; SP☆12参考表 8398a7 2014
p &copy; SP☆12参考表 2014
p = ENV.fetch('RELEASE', 'vx.y.z')
4 changes: 2 additions & 2 deletions app/views/layouts/_nav.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ nav.navbar.is-light aria-label='navigation' role='navigation'
.navbar-dropdown
= link_to 'ISTとの同期', ist_helps_path, class: 'navbar-item'
= link_to 'OAuth連携', oauth_helps_path, class: 'navbar-item'
- if user_signed_in? && current_user.member?
- if user_signed_in? && current_user.admin?
.navbar-item.has-dropdown.is-hoverable.admin-parent
a.navbar-link = icon 'cogs', '管理'
.navbar-dropdown
- if current_user.member?
- if current_user.admin?
= link_to '楽曲管理', admin_sheets_path, class: 'navbar-item'
- if current_user.admin?
= link_to 'ユーザ管理', admin_users_path, class: 'navbar-item'
Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ html
= javascript_pack_tag 'sentry'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
= javascript_pack_tag 'react'
script async=true src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
= csrf_meta_tags
- if Rails.env.production?
script async=true src='https://www.googletagmanager.com/gtag/js?id=UA-56797171-1'
Expand All @@ -26,6 +25,8 @@ html
gtag('config', 'UA-56797171-1');
gtag('set', { user_id: '#{current_user ? current_user.id : -1}' });
- if render_ads?
script async=true crossorigin='anonymous' src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5751776715932993'

= favicon_link_tag 'favicon.ico'

Expand Down
3 changes: 0 additions & 3 deletions app/views/logs/list.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ h2.subtitle.is-3 = icon 'sync', '更新履歴'
li = link_to 'ISTとの同期', ist_log_path, data: { disable_with: '同期中', confirm: 'ISTに登録していないと同期できません。同期しますか?' }, method: :post, remote: true

hr
- unless user_signed_in? && current_user.special?
= adsense
hr
section.accordions
article.accordion.is-primary
.accordion-header
Expand Down
3 changes: 0 additions & 3 deletions app/views/logs/sheet.html.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
h3.subtitle.is-3 = icon 'calendar-check-o', @title
hr
- unless user_signed_in? && current_user.special?
= adsense
hr
table.table.is-fullwidth.datatable
thead
th TITLE
Expand Down
8 changes: 2 additions & 6 deletions app/views/logs/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ p
| &nbsp;>&nbsp;
= link_to @next_update, logs_path(params[:id], @next_update)

- if user_signed_in? && (current_user.iidxid == params[:id] || current_user.owner?)
- if user_signed_in? && (current_user.iidxid == params[:id] || current_user.admin?)
p style='margin-bottom: 0px' 削除ボタン
= react_component_wrapper 'DestroyButtonDisplayToggle'
= react_component_wrapper 'TwitterSharedButton', text: "更新履歴(#{@logs.first.created_date})"
hr
- unless user_signed_in? && current_user.special?
= adsense
hr

section.accordions
article.accordion.is-primary
.accordion-header
Expand All @@ -40,7 +36,7 @@ table.table.is-fullwidth.datatable
- @logs.each do |log|
tr
td
- if user_signed_in? && (current_user.iidxid == params[:id] || current_user.owner?)
- if user_signed_in? && (current_user.iidxid == params[:id] || current_user.admin?)
= link_to log_path(log.id), method: :delete, remote: true, class: 'button is-danger is-small destroy-button', style: 'display: none', data: { confirm: '削除してよろしいですか?ランプは旧ランプの状態へ戻ります' }
= icon 'trash', '削除'
|&nbsp;&nbsp;
Expand Down
3 changes: 0 additions & 3 deletions app/views/recommends/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
h3.subtitle.is-3 = icon 'level-up-alt', @title
hr
- unless user_signed_in? && current_user.special?
= adsense
hr
table.table.is-fullwidth.datatable
thead
th.center TITLE
Expand Down
3 changes: 0 additions & 3 deletions app/views/sheets/power.html.slim
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
- @title = "#{User.find_by(iidxid: params[:iidxid]).djname} - 参考表"
h3.right サンプルデータはiidx.meより
hr
- unless user_signed_in? && current_user.special?
= adsense
hr
table.table.is-fullwidth.datatable
thead
tr
Expand Down
Loading

0 comments on commit b7ac270

Please sign in to comment.