forked from ifournight/tower_homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3904c4e
commit d44a9af
Showing
13 changed files
with
358 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
class AddTeamMember | ||
include ActiveModel::Model | ||
|
||
attr_accessor( | ||
:team_id, | ||
:authorizer_id, | ||
:member_id, | ||
:member_authority | ||
) | ||
|
||
validates :team_id, presence: true | ||
validates :authorizer_id, presence: true | ||
validates :member_id, presence: true | ||
validates :member_authority, presence: true | ||
validates :member_authority, inclusion: { in: TeamMembership::MEMBERSHIP_AUTHORITY.values } | ||
|
||
def do | ||
return nil if invalid? | ||
unless valid_team | ||
errors[:team_id] << 'Invalid team id' | ||
nil | ||
end | ||
unless valid_user(member_id) | ||
errors[:member_id] << 'Invalid memeber id' | ||
nil | ||
end | ||
unless valid_user(authorizer_id) | ||
errors[:authorizer_id] << 'Invalid authorizer_id' | ||
end | ||
|
||
@user = User.find(member_id) | ||
@authorizer = User.find(authorizer_id) | ||
@team = Team.find(team_id) | ||
|
||
if @team.members.include?(@user) | ||
errors[:member_id] << 'already a member' | ||
nil | ||
end | ||
|
||
unless check_authorizer_access | ||
errors[:authorizer_id] << "Authorizer doesn't have acess to add member" | ||
nil | ||
end | ||
|
||
add_team_member | ||
authority_team_memeber | ||
|
||
@user | ||
end | ||
|
||
def valid_team | ||
true | ||
end | ||
|
||
def valid_user(_user_id) | ||
true | ||
end | ||
|
||
def check_authorizer_access | ||
unless Access.has_access?(authorizer_id, team_id, 'Team', Access::ACCESS_TYPE[:TEAM_MEMBER_MANAGE]) | ||
errors[:authorizer_id] << | ||
"Authorizer(User) #{@authorizer.name} don't have access to add a member in team #{@team.name}." | ||
false | ||
end | ||
true | ||
end | ||
|
||
def add_team_member | ||
TeamMembership.create( | ||
team_id: team_id, | ||
member_id: member_id, | ||
member_authority: member_authority | ||
) | ||
end | ||
|
||
def authority_team_memeber | ||
accesses = [] | ||
case member_authority | ||
when TeamMembership::MEMBERSHIP_AUTHORITY[:ADMIN] | ||
accesses = Access::ACCESS_GROUP_ADMIN[:TEAM] | ||
when TeamMembership::MEMBERSHIP_AUTHORITY[:MEMBER] | ||
accesses = Access::ACCESS_GROUP_MEMBER[:TEAM] | ||
when TeamMemberShip::MEMBERSHIP_AUTHORITY[:GUEST] | ||
accesses = Access::ACCESS_GROUP_GUEST[:TEAM] | ||
end | ||
|
||
accesses.each do |access_type| | ||
Access.create( | ||
user_id: member_id, | ||
subject_id: @team.id, | ||
subject_type: @team.class.name, | ||
access_type: access_type | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class CreateProject | ||
include ActiveModel::Model | ||
|
||
validates :name, presence: true | ||
|
||
attr_accessor( | ||
:project_name, | ||
:desc, | ||
:team_id, | ||
:creator_id | ||
) | ||
|
||
def do | ||
unless user_valid? | ||
errors[:creator_id] << 'Invalid Creator ID' | ||
return nil | ||
end | ||
unless team_valid? | ||
errors[:team_id] << 'Invalid Team Invalid' | ||
return nil | ||
end | ||
|
||
@user = User.find(creator_id) | ||
@team = Team.find(team_id) | ||
|
||
return nil unless check_user_access | ||
|
||
@project = create_project | ||
make_creator_collaborator | ||
@project | ||
end | ||
|
||
private | ||
|
||
def user_valid? | ||
true | ||
end | ||
|
||
def team_valid? | ||
true | ||
end | ||
|
||
def check_user_access | ||
unless @team.members.include?(@user) | ||
errors[:creator_id] << | ||
"Creator(User) #{@user.name} can't create project being not a memeber of team #{@team.name}." | ||
return false | ||
end | ||
|
||
unless Access.has_access?(creator_id, team_id, 'Team', Access::ACCESS_TYPE[:CREATE_TEAM_PROJECTS]) | ||
errors[:creator_d] << | ||
"Creator(User) #{@user.name} don't have access to create a project." | ||
return false | ||
end | ||
|
||
true | ||
end | ||
|
||
def create_project | ||
Project.create( | ||
creator_id: creator_id, | ||
name: project_name, | ||
desc: desc, | ||
team_id: team_id | ||
) | ||
end | ||
|
||
def make_creator_collaborator | ||
accesses = Access::ACCESS_GROUP_MEMBER[:PROJECT] | ||
.dup | ||
.concat(Access::ACCESS_GROUP_PROJECT_MANAGER[:PROJECT].dup) | ||
accesses.each do |access_type| | ||
Access.create( | ||
user_id: @user.id, | ||
subject_id: @project.id, | ||
subject_type: @project.class.name, | ||
access_type: access_type | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class ProjectCollaboratorQuery | ||
def find_project_collaborators(project) | ||
temp = Access | ||
.where( | ||
subject_id: project.id, | ||
subject_type: project.class.name, | ||
access_type: Access::ACESS_TYPE[:PROJECT_COLLABORATOR] | ||
) | ||
.to_a | ||
if block_given? | ||
temp.each do |access| | ||
yield(access.user) | ||
end | ||
else | ||
temp.map(&:user) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.