/** * Prepare for the test, creating a user and entry to work with during the test. */ protected function setUp() { parent::setUp(); // Ensure that the entry was deleted in the case of a failed test. $this->beforeApplicationDestroyed(function () { $this->entry->delete(); $this->user->delete(); }); $this->user = factory(App\User::class)->create(['username' => 'test_user', 'email' => '*****@*****.**', 'password' => 'password']); $this->entry = \App\Project::create(['author' => $this->user->username, 'user_id' => $this->user->id, 'title' => 'Test Project', 'description' => 'For testing', 'body' => 'This is a simple test body']); $this->member = \App\ProjectMember::create(['user_id' => $this->user->id, 'project_id' => $this->entry->id, 'admin' => true]); $this->entry->members()->save($this->member); }
/** * Check if user is a admin of this project * * @return boolean */ public function isProjectAdmin($user_id = null) { if (!Auth::guest()) { if ($user_id == null) { $user_id = Auth::user()->id; } $isAdmin = ProjectMember::where('user_id', '=', $user_id)->where('project_id', '=', $this->id)->first(); if ($isAdmin != null) { $isAdmin = true; } else { $isAdmin = false; } } else { $isAdmin = false; } return $isAdmin; }
/** * Adds a member to the project * * @param boolean $admin * @param Int user_id * @return new member */ public function addMember($admin = false, $user_id = null) { if ($user_id == null) { $user_id = Auth::user()->id; } $member = ProjectMember::create(['user_id' => $user_id, 'project_id' => $this->id, 'admin' => $admin]); return $this->members()->save($member); }
/** * Helper function to format the min/max rules * * @param $field * @return string formatted (i.e. "min:2|max:20") */ private function betweenFormatter($field) { return 'between:' . ProjectMember::attributeLengths()[$field]['min'] . ',' . ProjectMember::attributeLengths()[$field]['max']; }
/** * Demote the given admin to member * * @param Project $project * @param ProjectMember $member * @return view Back to same view */ public function demoteFromAdmin(Project $project, ProjectMember $member) { $this->authorize('userIsOwner', $project); $this->authorize('userIsMember', [$project, $member]); $member->admin = false; $member->save(); return back(); }