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:
Luka Murn 2017-11-16 15:50:30 +01:00
parent ccef5571f1
commit d715eb06ef
4 changed files with 109 additions and 2 deletions

View 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

View 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

View file

@ -1,6 +1,6 @@
class User < ApplicationRecord
include SearchableModel
include SettingsModel
include SearchableModel, SettingsModel
include User::TeamRoles, User::ProjectRoles
acts_as_token_authenticatable
devise :invitable, :confirmable, :database_authenticatable, :registerable,

View file

@ -15,6 +15,9 @@ module Scinote
# Application configuration should go into files in config/initializers
# -- 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.active_job.queue_adapter = :delayed_job