mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-12-11 06:35:58 +08:00
Add team & project role-related helper functions to User model
This will be required later once we start using Canaid.
This commit is contained in:
parent
ccef5571f1
commit
d715eb06ef
4 changed files with 109 additions and 2 deletions
60
app/models/concerns/user/project_roles.rb
Normal file
60
app/models/concerns/user/project_roles.rb
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
require 'aspector'
|
||||||
|
|
||||||
|
module User::ProjectRoles
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
aspector do
|
||||||
|
# Check if user is member of project
|
||||||
|
around [
|
||||||
|
:is_member_of_project?,
|
||||||
|
:is_owner_of_project?,
|
||||||
|
:is_user_of_project?,
|
||||||
|
:is_user_or_higher_of_project?,
|
||||||
|
:is_technician_of_project?,
|
||||||
|
:is_technician_or_higher_of_project?,
|
||||||
|
:is_viewer_of_project?
|
||||||
|
] do |proxy, *args, &block|
|
||||||
|
if args[0]
|
||||||
|
@user_project = user_projects.where(project: args[0]).take
|
||||||
|
@user_project ? proxy.call(*args, &block) : false
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_member_of_project?(project)
|
||||||
|
# This is already checked by aspector, so just return true
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_creator_of_project?(project)
|
||||||
|
project.created_by == self
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_owner_of_project?(project)
|
||||||
|
@user_project.owner?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_user_of_project?(project)
|
||||||
|
@user_project.normal_user?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_user_or_higher_of_project?(project)
|
||||||
|
@user_project.normal_user? or @user_project.owner?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_technician_of_project?(project)
|
||||||
|
@user_project.technician?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_technician_or_higher_of_project?(project)
|
||||||
|
@user_project.technician? or
|
||||||
|
@user_project.normal_user? or
|
||||||
|
@user_project.owner?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_viewer_of_project?(project)
|
||||||
|
@user_project.viewer?
|
||||||
|
end
|
||||||
|
end
|
||||||
44
app/models/concerns/user/team_roles.rb
Normal file
44
app/models/concerns/user/team_roles.rb
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
require 'aspector'
|
||||||
|
|
||||||
|
module User::TeamRoles
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
aspector do
|
||||||
|
# Check if user is member of team
|
||||||
|
around [
|
||||||
|
:is_member_of_team?,
|
||||||
|
:is_admin_of_team?,
|
||||||
|
:is_normal_user_of_team?,
|
||||||
|
:is_normal_user_or_admin_of_team?,
|
||||||
|
:is_guest_of_team?
|
||||||
|
] do |proxy, *args, &block|
|
||||||
|
if args[0]
|
||||||
|
@user_team = user_teams.where(team: args[0]).take
|
||||||
|
@user_team ? proxy.call(*args, &block) : false
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_member_of_team?(team)
|
||||||
|
# This is already checked by aspector, so just return true
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_admin_of_team?(team)
|
||||||
|
@user_team.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_normal_user_of_team?(team)
|
||||||
|
@user_team.normal_user?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_normal_user_or_admin_of_team?(team)
|
||||||
|
@user_team.normal_user? or @user_team.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_guest_of_team?(team)
|
||||||
|
@user_team.guest?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
include SearchableModel
|
include SearchableModel, SettingsModel
|
||||||
include SettingsModel
|
include User::TeamRoles, User::ProjectRoles
|
||||||
|
|
||||||
acts_as_token_authenticatable
|
acts_as_token_authenticatable
|
||||||
devise :invitable, :confirmable, :database_authenticatable, :registerable,
|
devise :invitable, :confirmable, :database_authenticatable, :registerable,
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ module Scinote
|
||||||
# Application configuration should go into files in config/initializers
|
# Application configuration should go into files in config/initializers
|
||||||
# -- all .rb files in that directory are automatically loaded.
|
# -- all .rb files in that directory are automatically loaded.
|
||||||
|
|
||||||
|
# Load all model concerns, including subfolders
|
||||||
|
config.autoload_paths += Dir["#{Rails.root}/app/models/concerns/**/*.rb"]
|
||||||
|
|
||||||
config.encoding = 'utf-8'
|
config.encoding = 'utf-8'
|
||||||
|
|
||||||
config.active_job.queue_adapter = :delayed_job
|
config.active_job.queue_adapter = :delayed_job
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue