scinote-web/app/helpers/permission_helper.rb

1113 lines
29 KiB
Ruby
Raw Normal View History

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 [
: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]
@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
# ---- Almost everything is disabled for archived projects ----
around [
:can_view_project,
:can_view_project_activities,
:can_view_project_users,
:can_view_project_notifications,
:can_view_project_comments,
:can_edit_project,
:can_archive_project,
:can_add_user_to_project,
:can_remove_user_from_project,
:can_edit_users_on_project,
:can_add_comment_to_project,
:can_restore_archived_modules,
:can_view_project_samples,
:can_view_project_archive,
:can_create_new_tag,
:can_edit_tag,
:can_delete_tag,
:can_edit_canvas,
:can_reposition_modules,
:can_edit_connections,
:can_create_modules,
:can_edit_modules,
:can_clone_modules,
:can_archive_modules,
:can_view_reports,
:can_create_new_report,
2016-07-29 17:43:15 +08:00
:can_delete_reports,
:can_create_experiment
2016-02-12 23:52:43 +08:00
] do |proxy, *args, &block|
if args[0]
project = args[0]
project.active? ? proxy.call(*args, &block) : false
else
false
end
end
# ---- Almost everything is disabled for archived modules ----
around [
:can_view_module,
# TODO: Because module restoring is made via updating module attributes,
# (and that action checks if module is editable) this needs to be
# commented out or that functionality will not work any more.
#:can_edit_module,
:can_archive_module,
:can_edit_tags_for_module,
:can_add_tag_to_module,
:can_remove_tag_from_module,
:can_view_module_info,
:can_view_module_users,
:can_edit_users_on_module,
:can_add_user_to_module,
:can_remove_user_from_module,
2016-07-21 19:11:15 +08:00
:can_view_module_protocols,
:can_load_protocol_into_module,
:can_export_protocol_from_module,
:can_copy_protocol_to_repository,
2016-02-12 23:52:43 +08:00
:can_view_module_activities,
:can_view_module_comments,
:can_add_comment_to_module,
:can_view_module_samples,
:can_view_module_archive,
:can_view_results_in_module,
2016-07-21 19:11:15 +08:00
:can_view_or_download_result_assets,
2016-02-12 23:52:43 +08:00
:can_view_result_comments,
:can_add_result_comment_in_module,
:can_create_result_text_in_module,
:can_edit_result_text_in_module,
:can_archive_result_text_in_module,
:can_create_result_table_in_module,
:can_edit_result_table_in_module,
:can_archive_result_table_in_module,
:can_create_result_asset_in_module,
:can_edit_result_asset_in_module,
:can_archive_result_asset_in_module,
:can_add_samples_to_module,
2016-07-29 17:43:15 +08:00
:can_delete_samples_from_module
] do |proxy, *args, &block|
if args[0]
my_module = args[0]
if my_module.active? &&
my_module.experiment.active? &&
my_module.experiment.project.active?
proxy.call(*args, &block)
else
false
end
else
false
end
end
# ---- Some things are disabled for archived experiment ----
2016-07-29 17:43:15 +08:00
around [
:can_edit_experiment,
:can_view_experiment,
:can_view_experiment_archive,
:can_archive_experiment,
:can_view_experiment_samples,
:can_clone_experiment,
:can_move_experiment,
:can_edit_canvas,
:can_reposition_modules,
:can_edit_connections,
:can_create_modules,
:can_edit_modules,
:can_clone_modules,
:can_archive_modules
2016-02-12 23:52:43 +08:00
] do |proxy, *args, &block|
if args[0]
2016-07-29 17:43:15 +08:00
experiment = args[0]
if experiment.active? &&
experiment.project.active?
2016-02-12 23:52:43 +08:00
proxy.call(*args, &block)
else
false
end
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 ----
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
def is_admin_of_team(team)
@user_team.admin?
2016-02-12 23:52:43 +08:00
end
def is_normal_user_of_team(team)
@user_team.normal_user?
2016-02-12 23:52:43 +08:00
end
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
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
@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).
# ---- ATWHO PERMISSIONS ----
def can_view_team_users(team)
is_member_of_team(team)
end
2016-02-12 23:52:43 +08:00
# ---- PROJECT PERMISSIONS ----
def can_view_projects(team)
is_member_of_team(team)
2016-02-12 23:52:43 +08:00
end
def can_create_project(team)
is_normal_user_or_admin_of_team(team)
2016-02-12 23:52:43 +08:00
end
# User can view project if he's assigned onto it, or if
# a project is public/visible, and user is a member of that team
2016-02-12 23:52:43 +08:00
def can_view_project(project)
is_admin_of_team(project.team) ||
is_member_of_project(project) ||
(project.visible? and is_member_of_team(project.team))
2016-02-12 23:52:43 +08:00
end
def can_view_project_activities(project)
is_member_of_project(project)
end
def can_view_project_users(project)
can_view_project(project)
end
def can_view_project_notifications(project)
can_view_project(project)
end
def can_view_project_comments(project)
can_view_project(project)
end
def can_edit_project(project)
is_owner_of_project(project)
end
def can_archive_project(project)
2016-08-08 20:19:35 +08:00
is_owner_of_project(project)
2016-02-12 23:52:43 +08:00
end
def can_restore_project(project)
2016-08-08 20:19:35 +08:00
project.archived? && is_owner_of_project(project)
2016-02-12 23:52:43 +08:00
end
def can_add_user_to_project(project)
is_owner_of_project(project)
end
def can_remove_user_from_project(project)
is_owner_of_project(project)
end
def can_edit_users_on_project(project)
is_owner_of_project(project)
end
def can_add_comment_to_project(project)
is_technician_or_higher_of_project(project)
end
def can_edit_project_comment(comment)
comment.project.present? &&
(
comment.user == current_user ||
is_owner_of_project(comment.project)
)
end
def can_delete_project_comment(comment)
comment.project.present? &&
(
comment.user == current_user ||
is_owner_of_project(comment.project)
)
end
2016-02-12 23:52:43 +08:00
def can_restore_archived_modules(project)
is_user_or_higher_of_project(project)
end
def can_view_project_samples(project)
can_view_samples(project.team)
2016-02-12 23:52:43 +08:00
end
def can_view_project_archive(project)
is_user_or_higher_of_project(project)
end
def can_create_new_tag(project)
is_user_or_higher_of_project(project)
end
def can_edit_tag(project)
is_user_or_higher_of_project(project)
end
def can_delete_tag(project)
is_user_or_higher_of_project(project)
end
2016-07-26 17:43:39 +08:00
# ---- EXPERIMENT PERMISSIONS ----
def can_view_experiment_actions(experiment)
2017-03-10 00:45:34 +08:00
can_edit_experiment(experiment) ||
can_archive_experiment(experiment)
end
2016-07-26 17:43:39 +08:00
def can_create_experiment(project)
is_user_or_higher_of_project(project)
end
2016-07-29 17:43:15 +08:00
def can_edit_experiment(experiment)
is_user_or_higher_of_project(experiment.project)
2016-07-26 17:43:39 +08:00
end
def can_view_experiment(experiment)
can_view_project(experiment.project)
end
def can_view_experiment_archive(experiment)
can_view_project(experiment.project)
end
2016-07-29 17:43:15 +08:00
def can_archive_experiment(experiment)
is_user_or_higher_of_project(experiment.project)
2016-07-26 17:43:39 +08:00
end
def can_restore_experiment(experiment)
2016-08-03 15:34:10 +08:00
experiment.archived? && is_user_or_higher_of_project(experiment.project)
2016-07-26 17:43:39 +08:00
end
def can_view_experiment_samples(experiment)
can_view_samples(experiment.project.team)
end
def can_clone_experiment(experiment)
is_user_or_higher_of_project(experiment.project) &&
is_normal_user_or_admin_of_team(experiment.project.team)
end
def can_move_experiment(experiment)
is_user_or_higher_of_project(experiment.project) &&
is_normal_user_or_admin_of_team(experiment.project.team)
end
2016-02-12 23:52:43 +08:00
# ---- WORKFLOW PERMISSIONS ----
def can_edit_canvas(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-07-27 17:35:07 +08:00
def can_reposition_modules(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-07-27 17:35:07 +08:00
def can_edit_connections(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
# ---- MODULE PERMISSIONS ----
2016-07-27 17:35:07 +08:00
def can_create_modules(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-07-27 17:35:07 +08:00
def can_edit_modules(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-07-27 17:35:07 +08:00
def can_clone_modules(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-08-17 19:48:02 +08:00
def can_move_modules(experiment)
is_user_or_higher_of_project(experiment.project)
end
2016-07-27 17:35:07 +08:00
def can_archive_modules(experiment)
is_user_or_higher_of_project(experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_view_module(my_module)
2016-07-22 20:31:09 +08:00
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_module(my_module)
2016-07-22 20:31:09 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_archive_module(my_module)
2016-07-28 16:09:05 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_restore_module(my_module)
2016-08-08 21:04:02 +08:00
my_module.archived? &&
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_tags_for_module(my_module)
2016-07-22 20:31:09 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_add_tag_to_module(my_module)
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_remove_tag_from_module(my_module)
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_view_module_info(my_module)
2016-07-22 20:31:09 +08:00
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_view_module_users(my_module)
2016-07-22 20:31:09 +08:00
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_users_on_module(my_module)
2016-07-25 15:31:40 +08:00
is_owner_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_add_user_to_module(my_module)
2016-07-25 15:31:40 +08:00
is_owner_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_remove_user_from_module(my_module)
2016-07-25 15:31:40 +08:00
is_owner_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-07-21 19:11:15 +08:00
def can_view_module_protocols(my_module)
can_view_module(my_module)
end
2016-02-12 23:52:43 +08:00
def can_view_module_activities(my_module)
2016-07-22 20:31:09 +08:00
is_member_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_view_module_comments(my_module)
2016-07-22 20:31:09 +08:00
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_add_comment_to_module(my_module)
2016-07-22 20:31:09 +08:00
is_technician_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_module_comment(comment)
comment.my_module.present? &&
(
comment.user == current_user ||
is_owner_of_project(
comment.my_module.experiment.project
)
)
end
def can_delete_module_comment(comment)
comment.my_module.present? &&
(
comment.user == current_user ||
is_owner_of_project(
comment.my_module.experiment.project
)
)
end
2016-02-12 23:52:43 +08:00
def can_view_module_samples(my_module)
can_view_module(my_module) and
can_view_samples(my_module.experiment.project.team)
2016-02-12 23:52:43 +08:00
end
def can_view_module_archive(my_module)
2016-07-22 20:31:09 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_complete_module(my_module)
is_technician_or_higher_of_project(my_module.experiment.project)
end
2016-02-12 23:52:43 +08:00
# ---- RESULTS PERMISSIONS ----
def can_view_results_in_module(my_module)
2016-07-25 15:31:40 +08:00
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
2016-07-21 19:11:15 +08:00
def can_view_or_download_result_assets(my_module)
is_member_of_project(my_module.experiment.project) ||
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_view_result_comments(my_module)
2016-07-25 15:31:40 +08:00
can_view_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_add_result_comment_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_technician_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_result_comment_in_module(comment)
comment.result.present? &&
(
comment.user == current_user ||
is_owner_of_project(
comment.result.my_module.experiment.project
)
)
end
def can_delete_result_comment_in_module(comment)
comment.result.present? &&
(
comment.user == current_user ||
is_owner_of_project(
comment.result.my_module.experiment.project
)
)
end
def can_delete_module_result(result)
is_owner_of_project(result.my_module.experiment.project)
end
2016-02-12 23:52:43 +08:00
# ---- RESULT TEXT PERMISSIONS ----
def can_create_result_text_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_result_text_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_archive_result_text_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_owner_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
# ---- RESULT TABLE PERMISSIONS ----
def can_create_result_table_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_result_table_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_archive_result_table_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_owner_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
# ---- RESULT ASSET PERMISSIONS ----
def can_create_result_asset_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_edit_result_asset_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_archive_result_asset_in_module(my_module)
2016-07-25 15:31:40 +08:00
is_owner_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
# ---- REPORTS PERMISSIONS ----
def can_view_reports(project)
can_view_project(project)
end
def can_create_new_report(project)
is_technician_or_higher_of_project(project)
end
def can_delete_reports(project)
is_technician_or_higher_of_project(project)
end
# ---- SAMPLE PERMISSIONS ----
def can_create_samples(team)
is_normal_user_or_admin_of_team(team)
2016-02-12 23:52:43 +08:00
end
def can_view_samples(team)
is_member_of_team(team)
2016-02-12 23:52:43 +08:00
end
# Only person who created the sample
# or team admin can edit it
2016-02-12 23:52:43 +08:00
def can_edit_sample(sample)
is_admin_of_team(sample.team) or
sample.user == current_user
2016-02-12 23:52:43 +08:00
end
# Only person who created sample can delete it
def can_delete_sample(sample)
sample.user == current_user
end
def can_delete_samples(team)
is_normal_user_or_admin_of_team(team)
2016-02-12 23:52:43 +08:00
end
def can_add_samples_to_module(my_module)
2016-07-25 15:31:40 +08:00
is_technician_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
def can_delete_samples_from_module(my_module)
2016-07-25 15:31:40 +08:00
is_technician_or_higher_of_project(my_module.experiment.project)
2016-02-12 23:52:43 +08:00
end
# ---- SAMPLE TYPES PERMISSIONS ----
def can_create_sample_type_in_team(team)
is_normal_user_or_admin_of_team(team)
2016-02-12 23:52:43 +08:00
end
# ---- SAMPLE GROUPS PERMISSIONS ----
def can_create_sample_group_in_team(team)
is_normal_user_or_admin_of_team(team)
2016-02-12 23:52:43 +08:00
end
# ---- CUSTOM FIELDS PERMISSIONS ----
def can_create_custom_field_in_team(team)
is_normal_user_or_admin_of_team(team)
2016-02-12 23:52:43 +08:00
end
def can_edit_custom_field(custom_field)
custom_field.user == current_user ||
is_admin_of_team(custom_field.team)
end
def can_delete_custom_field(custom_field)
custom_field.user == current_user ||
is_admin_of_team(custom_field.team)
end
2016-07-21 19:11:15 +08:00
# ---- PROTOCOL PERMISSIONS ----
def can_view_team_protocols(team)
is_member_of_team(team)
2016-07-21 19:11:15 +08:00
end
def can_create_new_protocol(team)
is_normal_user_or_admin_of_team(team)
2016-07-21 19:11:15 +08:00
end
def can_import_protocols(team)
is_normal_user_or_admin_of_team(team)
2016-07-21 19:11:15 +08:00
end
def can_view_protocol(protocol)
if protocol.in_repository_public?
is_member_of_team(protocol.team)
elsif protocol.in_repository_private? or protocol.in_repository_archived?
is_member_of_team(protocol.team) and
protocol.added_by == current_user
2016-07-21 19:11:15 +08:00
elsif protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
can_view_module(my_module) &&
my_module.experiment.active?
2016-07-21 19:11:15 +08:00
else
false
end
end
def can_edit_protocol(protocol)
is_normal_user_or_admin_of_team(protocol.team) and
2016-07-21 19:11:15 +08:00
current_user == protocol.added_by and (not protocol.in_repository_archived?)
end
def can_clone_protocol(protocol)
is_normal_user_or_admin_of_team(protocol.team) and
2016-07-21 19:11:15 +08:00
(
protocol.in_repository_public? or
(protocol.in_repository_private? and current_user == protocol.added_by)
)
end
def can_make_protocol_private(protocol)
protocol.added_by == current_user and
protocol.in_repository_public?
end
def can_publish_protocol(protocol)
protocol.added_by == current_user and
protocol.in_repository_private?
end
def can_export_protocol(protocol)
(protocol.in_repository_public? and is_member_of_team(protocol.team)) or
(protocol.in_repository_private? and protocol.added_by == current_user) or
(protocol.in_module? and
can_export_protocol_from_module(protocol.my_module))
2016-07-21 19:11:15 +08:00
end
def can_archive_protocol(protocol)
protocol.added_by == current_user and
(protocol.in_repository_public? or protocol.in_repository_private?)
2016-07-21 19:11:15 +08:00
end
def can_restore_protocol(protocol)
protocol.added_by == current_user and
protocol.in_repository_archived?
2016-07-21 19:11:15 +08:00
end
def can_unlink_protocol(protocol)
if protocol.linked?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
is_user_or_higher_of_project(my_module.experiment.project) &&
my_module.experiment.active?
2016-07-21 19:11:15 +08:00
else
false
end
end
def can_revert_protocol(protocol)
if protocol.linked?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
is_user_or_higher_of_project(my_module.experiment.project) &&
my_module.experiment.active?
2016-07-21 19:11:15 +08:00
else
false
end
end
def can_update_protocol_from_parent(protocol)
if protocol.linked?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
is_user_or_higher_of_project(my_module.experiment.project) &&
my_module.experiment.active?
2016-07-21 19:11:15 +08:00
else
false
end
end
def can_load_protocol_from_repository(protocol, source)
if can_view_protocol(source)
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
is_user_or_higher_of_project(my_module.experiment.project) &&
my_module.experiment.active?
2016-07-21 19:11:15 +08:00
else
false
end
end
def can_update_parent_protocol(protocol)
if protocol.linked?
my_module = protocol.my_module
parent = protocol.parent
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_normal_user_or_admin_of_team(parent.team) &&
is_user_or_higher_of_project(my_module.experiment.project) &&
(parent.in_repository_public? or parent.in_repository_private?) &&
parent.added_by == current_user
2016-07-21 19:11:15 +08:00
else
false
end
end
# ---- STEPS PERMISSIONS ----
def can_load_protocol_into_module(my_module)
2016-07-25 15:31:40 +08:00
is_user_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
end
def can_export_protocol_from_module(my_module)
can_view_module_protocols(my_module)
end
def can_copy_protocol_to_repository(my_module)
is_normal_user_or_admin_of_team(my_module.experiment.project.team)
2016-07-21 19:11:15 +08:00
end
def can_link_copied_protocol_in_repository(protocol)
can_copy_protocol_to_repository(protocol.my_module) and
is_user_or_higher_of_project(protocol.my_module.experiment.project)
2016-07-21 19:11:15 +08:00
end
def can_view_steps_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
can_view_module(my_module)
2016-07-21 19:11:15 +08:00
elsif protocol.in_repository?
protocol.in_repository_active? and can_view_protocol(protocol)
else
false
end
end
def can_create_step_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_user_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
elsif protocol.in_repository?
protocol.in_repository_active? and can_edit_protocol(protocol)
else
false
end
end
def can_reorder_step_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_user_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
elsif protocol.in_repository?
protocol.in_repository_active? and can_edit_protocol(protocol)
else
false
end
end
# Could possibly be divided into:
# - edit step name/description
# - adding checklists
# - adding assets
# - adding tables
# but right now we have 1 page to rule them all.
def can_edit_step_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_user_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
elsif protocol.in_repository?
protocol.in_repository_active? and can_edit_protocol(protocol)
else
false
end
end
def can_delete_step_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_owner_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
elsif protocol.in_repository?
protocol.in_repository_active? and can_edit_protocol(protocol)
else
false
end
end
def can_view_step_comments(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
can_view_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
else
# In repository, comments are disabled
false
end
end
def can_add_step_comment_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_technician_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
else
# In repository, user cannot complete steps
false
end
end
def can_edit_step_comment_in_protocol(comment)
return false if comment.step.blank?
protocol = comment.step.protocol
if protocol.in_module?
comment.user == current_user ||
is_owner_of_project(
protocol.my_module.experiment.project
)
else
false
end
end
def can_delete_step_comment_in_protocol(comment)
return false if comment.step.blank?
protocol = comment.step.protocol
if protocol.in_module?
comment.user == current_user ||
is_owner_of_project(
protocol.my_module.experiment.project
)
else
false
end
end
2016-07-21 19:11:15 +08:00
def can_view_or_download_step_assets(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
(is_member_of_project(my_module.experiment.project) ||
can_view_project(my_module.experiment.project))
2016-07-21 19:11:15 +08:00
elsif protocol.in_repository?
protocol.in_repository_active? and can_view_protocol(protocol)
else
false
end
end
def can_complete_step_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_technician_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
else
# In repository, user cannot complete steps
false
end
end
def can_uncomplete_step_in_protocol(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_user_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
else
# In repository, user cannot complete steps
false
end
end
def can_check_checkbox(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_technician_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
else
# In repository, user cannot check checkboxes
false
end
end
def can_uncheck_checkbox(protocol)
if protocol.in_module?
my_module = protocol.my_module
2016-07-25 18:07:10 +08:00
my_module.active? &&
my_module.experiment.project.active? &&
my_module.experiment.active? &&
is_user_or_higher_of_project(my_module.experiment.project)
2016-07-21 19:11:15 +08:00
else
# In repository, user cannot check checkboxes
false
end
end
# ---- REPOSITORIES PERMISSIONS ----
def can_view_team_repositories(team)
is_member_of_team(team)
2017-05-23 17:32:02 +08:00
end
2017-06-06 21:50:43 +08:00
def can_create_repository(team)
2017-06-06 19:33:57 +08:00
is_admin_of_team(team) &&
team.repositories.count < Constants::REPOSITORIES_LIMIT
2017-05-24 15:29:44 +08:00
end
def can_view_repository(repository)
is_member_of_team(repository.team)
end
def can_edit_and_destroy_repository(repository)
is_admin_of_team(repository.team)
end
2017-06-06 23:35:29 +08:00
2017-06-08 00:43:48 +08:00
def can_copy_repository(repository)
can_create_repository(repository.team)
end
2017-06-06 23:35:29 +08:00
def can_create_columns_in_repository(repository)
is_normal_user_or_admin_of_team(repository.team)
end
2017-06-07 23:07:28 +08:00
def can_delete_column_in_repository(column)
column.created_by == current_user ||
is_admin_of_team(column.repository.team)
2017-06-06 23:35:29 +08:00
end
2017-06-07 23:07:28 +08:00
def can_edit_column_in_repository(column)
column.created_by == current_user ||
is_admin_of_team(column.repository.team)
2017-06-06 23:35:29 +08:00
end
def can_create_repository_records(repository)
is_normal_user_or_admin_of_team(repository.team)
end
2017-06-13 14:10:10 +08:00
def can_import_repository_records(repository)
is_normal_user_or_admin_of_team(repository.team)
end
2017-06-07 23:07:28 +08:00
def can_edit_repository_record(record)
is_normal_user_or_admin_of_team(record.repository.team)
2017-06-06 23:35:29 +08:00
end
def can_delete_repository_records(repository)
is_normal_user_or_admin_of_team(repository.team)
end
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
def can_assign_repository_records(my_module, repository)
2017-06-07 23:07:28 +08:00
can_delete_repository_records(repository) &&
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-06-07 23:07:28 +08:00
can_delete_repository_records(repository) &&
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