2016-02-12 23:52:43 +08:00
|
|
|
require "aspector"
|
|
|
|
|
|
|
|
module PermissionHelper
|
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# SOME REFLECTION MAGIC
|
|
|
|
#######################################################
|
|
|
|
aspector do
|
2017-01-25 19:30:11 +08:00
|
|
|
# ---- TEAM ROLES DEFINITIONS ----
|
2016-02-12 23:52:43 +08:00
|
|
|
around [
|
2017-01-25 16:48:49 +08:00
|
|
|
:is_member_of_team,
|
|
|
|
:is_admin_of_team,
|
|
|
|
:is_normal_user_of_team,
|
|
|
|
:is_normal_user_or_admin_of_team,
|
|
|
|
:is_guest_of_team
|
2016-02-12 23:52:43 +08:00
|
|
|
] do |proxy, *args, &block|
|
|
|
|
if args[0]
|
2017-01-25 16:48:49 +08:00
|
|
|
@user_team = current_user.user_teams.where(team: args[0]).take
|
|
|
|
@user_team ? proxy.call(*args, &block) : false
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ---- PROJECT ROLES DEFINITIONS ----
|
|
|
|
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 = current_user.user_projects.where(project: args[0]).take
|
|
|
|
@user_project ? proxy.call(*args, &block) : false
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# ROLES
|
|
|
|
#######################################################
|
|
|
|
# The following code should stay private, and for each
|
|
|
|
# permission that's needed throughout application, a
|
|
|
|
# public method should be made. That way, we can have
|
|
|
|
# all permissions gathered here in one place.
|
|
|
|
|
2017-01-25 19:30:11 +08:00
|
|
|
# ---- TEAM ROLES ----
|
2017-01-25 16:48:49 +08:00
|
|
|
def is_member_of_team(team)
|
2016-02-12 23:52:43 +08:00
|
|
|
# This is already checked by aspector, so just return true
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-01-25 16:48:49 +08:00
|
|
|
def is_admin_of_team(team)
|
|
|
|
@user_team.admin?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-01-25 16:48:49 +08:00
|
|
|
def is_normal_user_of_team(team)
|
|
|
|
@user_team.normal_user?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-01-25 16:48:49 +08:00
|
|
|
def is_normal_user_or_admin_of_team(team)
|
|
|
|
@user_team.normal_user? or @user_team.admin?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-01-25 16:48:49 +08:00
|
|
|
def is_guest_of_team(team)
|
|
|
|
@user_team.guest?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# ---- PROJECT ROLES ----
|
|
|
|
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 == current_user
|
|
|
|
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
|
2017-01-25 16:48:49 +08:00
|
|
|
@user_project.normal_user? or
|
|
|
|
@user_project.owner?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_viewer_of_project(project)
|
|
|
|
@user_project.viewer?
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
|
|
|
|
#######################################################
|
|
|
|
# PERMISSIONS
|
|
|
|
#######################################################
|
|
|
|
# The following list can be expanded for new permissions,
|
|
|
|
# and only the following list should be public. Also,
|
|
|
|
# in a lot of cases, the following methods should be added
|
|
|
|
# to "is project archived" or "is module archived" checks
|
|
|
|
# at the beginning of this file (via aspector).
|
|
|
|
|
2017-01-04 17:54:02 +08:00
|
|
|
# ---- ATWHO PERMISSIONS ----
|
2017-12-13 21:43:11 +08:00
|
|
|
# def can_view_team_users(team)
|
|
|
|
# is_member_of_team(team)
|
|
|
|
# end
|
2017-01-04 17:54:02 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# ---- PROJECT PERMISSIONS ----
|
|
|
|
|
2017-11-27 18:24:41 +08:00
|
|
|
# def can_view_projects(team)
|
|
|
|
# is_member_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-12-08 00:08:41 +08:00
|
|
|
# def can_create_project(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# ---- REPORTS PERMISSIONS ----
|
|
|
|
|
|
|
|
# ---- SAMPLE PERMISSIONS ----
|
|
|
|
|
2017-12-04 23:45:23 +08:00
|
|
|
# def can_create_samples(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-12-07 20:35:15 +08:00
|
|
|
# def can_view_samples(team)
|
|
|
|
# is_member_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Only person who created the sample
|
2017-01-25 16:48:49 +08:00
|
|
|
# or team admin can edit it
|
2017-12-07 20:49:44 +08:00
|
|
|
# def can_edit_sample(sample)
|
|
|
|
# is_admin_of_team(sample.team) or
|
|
|
|
# sample.user == current_user
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Only person who created sample can delete it
|
2017-12-07 23:40:27 +08:00
|
|
|
# def can_delete_sample(sample)
|
|
|
|
# sample.user == current_user
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-12-07 23:40:27 +08:00
|
|
|
# def can_delete_samples(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# ---- SAMPLE TYPES PERMISSIONS ----
|
|
|
|
|
2017-12-08 23:40:08 +08:00
|
|
|
# def can_create_sample_type_in_team(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# ---- SAMPLE GROUPS PERMISSIONS ----
|
|
|
|
|
2017-12-08 23:40:08 +08:00
|
|
|
# def can_create_sample_group_in_team(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# ---- CUSTOM FIELDS PERMISSIONS ----
|
|
|
|
|
2017-12-08 23:40:08 +08:00
|
|
|
# def can_create_custom_field_in_team(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-12-08 23:40:08 +08:00
|
|
|
# def can_edit_custom_field(custom_field)
|
|
|
|
# custom_field.user == current_user ||
|
|
|
|
# is_admin_of_team(custom_field.team)
|
|
|
|
# end
|
2016-11-29 18:43:18 +08:00
|
|
|
|
2017-12-08 23:40:08 +08:00
|
|
|
# def can_delete_custom_field(custom_field)
|
|
|
|
# custom_field.user == current_user ||
|
|
|
|
# is_admin_of_team(custom_field.team)
|
|
|
|
# end
|
2016-11-29 18:43:18 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
# ---- PROTOCOL PERMISSIONS ----
|
|
|
|
|
2017-11-27 18:24:41 +08:00
|
|
|
# def can_view_team_protocols(team)
|
|
|
|
# is_member_of_team(team)
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-04 20:07:23 +08:00
|
|
|
# def can_create_new_protocol(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-04 20:07:23 +08:00
|
|
|
# def can_import_protocols(team)
|
|
|
|
# is_normal_user_or_admin_of_team(team)
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-07 00:23:08 +08:00
|
|
|
# def can_edit_protocol(protocol)
|
|
|
|
# is_normal_user_or_admin_of_team(protocol.team) and
|
|
|
|
# current_user == protocol.added_by and (not protocol.in_repository_archived?)
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-07 18:11:27 +08:00
|
|
|
# def can_clone_protocol(protocol)
|
|
|
|
# is_normal_user_or_admin_of_team(protocol.team) and
|
|
|
|
# (
|
|
|
|
# protocol.in_repository_public? or
|
|
|
|
# (protocol.in_repository_private? and current_user == protocol.added_by)
|
|
|
|
# )
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-07 01:40:49 +08:00
|
|
|
# def can_make_protocol_private(protocol)
|
|
|
|
# protocol.added_by == current_user and
|
|
|
|
# protocol.in_repository_public?
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-07 01:40:49 +08:00
|
|
|
# def can_publish_protocol(protocol)
|
|
|
|
# protocol.added_by == current_user and
|
|
|
|
# protocol.in_repository_private?
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-07 01:40:49 +08:00
|
|
|
# def can_archive_protocol(protocol)
|
|
|
|
# protocol.added_by == current_user and
|
|
|
|
# (protocol.in_repository_public? or protocol.in_repository_private?)
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-12-07 01:40:49 +08:00
|
|
|
# def can_restore_protocol(protocol)
|
|
|
|
# protocol.added_by == current_user and
|
|
|
|
# protocol.in_repository_archived?
|
|
|
|
# end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
# ---- REPOSITORIES PERMISSIONS ----
|
|
|
|
|
2017-12-12 18:45:19 +08:00
|
|
|
# def can_view_team_repositories(team)
|
|
|
|
# is_member_of_team(team)
|
|
|
|
# end
|
2017-05-23 17:05:25 +08:00
|
|
|
|
2017-12-13 18:09:25 +08:00
|
|
|
# def can_create_repository(team)
|
|
|
|
# is_admin_of_team(team) &&
|
|
|
|
# team.repositories.count < Constants::REPOSITORIES_LIMIT
|
|
|
|
# end
|
2017-05-24 15:29:44 +08:00
|
|
|
|
2017-12-12 21:56:07 +08:00
|
|
|
# def can_view_repository(repository)
|
|
|
|
# is_member_of_team(repository.team)
|
|
|
|
# end
|
2017-06-01 02:56:05 +08:00
|
|
|
|
2017-12-13 18:09:25 +08:00
|
|
|
# def can_edit_and_destroy_repository(repository)
|
|
|
|
# is_admin_of_team(repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-13 18:09:25 +08:00
|
|
|
# def can_copy_repository(repository)
|
|
|
|
# can_create_repository(repository.team)
|
|
|
|
# end
|
2017-06-08 00:43:48 +08:00
|
|
|
|
2017-12-12 22:35:43 +08:00
|
|
|
# def can_create_columns_in_repository(repository)
|
|
|
|
# is_normal_user_or_admin_of_team(repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-12 22:35:43 +08:00
|
|
|
# def can_delete_column_in_repository(column)
|
|
|
|
# column.created_by == current_user ||
|
|
|
|
# is_admin_of_team(column.repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-12 22:35:43 +08:00
|
|
|
# def can_edit_column_in_repository(column)
|
|
|
|
# column.created_by == current_user ||
|
|
|
|
# is_admin_of_team(column.repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-12 21:30:28 +08:00
|
|
|
# def can_create_repository_records(repository)
|
|
|
|
# is_normal_user_or_admin_of_team(repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-12 21:30:28 +08:00
|
|
|
# def can_import_repository_records(repository)
|
|
|
|
# is_normal_user_or_admin_of_team(repository.team)
|
|
|
|
# end
|
2017-06-13 14:10:10 +08:00
|
|
|
|
2017-12-12 21:30:28 +08:00
|
|
|
# def can_edit_repository_record(record)
|
|
|
|
# is_normal_user_or_admin_of_team(record.repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-12 21:30:28 +08:00
|
|
|
# def can_delete_repository_records(repository)
|
|
|
|
# is_normal_user_or_admin_of_team(repository.team)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2017-12-12 21:30:28 +08:00
|
|
|
# def can_delete_repository_record(record)
|
|
|
|
# team = record.repository.team
|
|
|
|
# is_admin_of_team(team) || (is_normal_user_of_team(team) &&
|
|
|
|
# record.created_by == current_user)
|
|
|
|
# end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
|
|
|
def can_assign_repository_records(my_module, repository)
|
2017-12-12 21:30:28 +08:00
|
|
|
is_normal_user_or_admin_of_team(repository.team) &&
|
2017-06-06 23:35:29 +08:00
|
|
|
is_technician_or_higher_of_project(my_module.experiment.project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_unassign_repository_records(my_module, repository)
|
2017-12-12 21:30:28 +08:00
|
|
|
is_normal_user_or_admin_of_team(repository.team) &&
|
2017-06-06 23:35:29 +08:00
|
|
|
is_technician_or_higher_of_project(my_module.experiment.project)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|