diff --git a/app/models/concerns/user/project_roles.rb b/app/models/concerns/user/project_roles.rb new file mode 100644 index 000000000..da8e4fb76 --- /dev/null +++ b/app/models/concerns/user/project_roles.rb @@ -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 \ No newline at end of file diff --git a/app/models/concerns/user/team_roles.rb b/app/models/concerns/user/team_roles.rb new file mode 100644 index 000000000..068c5713c --- /dev/null +++ b/app/models/concerns/user/team_roles.rb @@ -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 \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 96afe98b2..6d76d455b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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, diff --git a/config/application.rb b/config/application.rb index 014a70fbb..129ba8e4c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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