2017-11-16 22:50:30 +08:00
|
|
|
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|
|
2017-11-16 22:50:30 +08:00
|
|
|
if args[0]
|
2021-08-26 22:56:21 +08:00
|
|
|
@user_project = user_projects.find_by(project: args[0])
|
2017-11-16 22:50:30 +08:00
|
|
|
@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)
|
2021-08-26 22:56:21 +08:00
|
|
|
# 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
|
2017-11-16 22:50:30 +08:00
|
|
|
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
|
2018-01-20 01:25:58 +08:00
|
|
|
end
|