scinote-web/app/models/concerns/user/project_roles.rb

66 lines
1.5 KiB
Ruby
Raw Normal View History

require 'aspector'
module User::ProjectRoles
extend ActiveSupport::Concern
aspector do
# Check if user is member of project
2017-11-23 18:33:39 +08:00
around %i(
is_member_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.find_by(project: args[0])
@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)
# if project has no assigned users, creator can manage it
if project.user_projects.none? && project.created_by_id == id
true
else
user_project = user_projects.find_by(project: project)
user_project.present? ? user_project.owner? : false
end
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