2021-09-09 21:41:42 +08:00
|
|
|
module PermissionHelpers
|
|
|
|
def create_user_assignment(object, role, user, assigned_by = nil)
|
2022-06-07 00:21:57 +08:00
|
|
|
user_assignment = UserAssignment.where(assignable: object, user: user, user_role: role).take
|
2021-09-14 19:30:52 +08:00
|
|
|
|
2022-06-07 00:21:57 +08:00
|
|
|
if user_assignment.blank?
|
|
|
|
user_assignment = create :user_assignment,
|
|
|
|
assignable: object,
|
|
|
|
user: user,
|
|
|
|
user_role: role,
|
|
|
|
assigned_by: assigned_by || user
|
2021-09-14 19:30:52 +08:00
|
|
|
end
|
2021-09-09 21:41:42 +08:00
|
|
|
|
|
|
|
case object
|
|
|
|
when MyModule
|
|
|
|
create_user_assignment(object.experiment, role, user, assigned_by)
|
|
|
|
when Experiment
|
|
|
|
create_user_assignment(object.project, role, user, assigned_by)
|
|
|
|
end
|
2022-06-07 00:21:57 +08:00
|
|
|
|
|
|
|
user_assignment
|
2021-09-09 21:41:42 +08:00
|
|
|
end
|
|
|
|
end
|