/** * Add project member. * * @return \Avalon\Http\RedirectResponse|\Avalon\Http\Response */ public function createAction() { $errors = []; $user = User::find('username', Request::$post->get('username')); $role = ProjectRole::find(Request::$post->get('role_id')); // Check if they entered a username if (!Request::$post->has('username') || Request::$post->get('username') == '') { $errors['username'] = $this->translate('errors.validations.required', ['field' => $this->translate('username')]); } elseif (!$user) { $errors['username'] = $this->translate('errors.users.doesnt_exist'); } // Check if the user is already a member of the project if ($user) { $member = UserRole::select('id')->where('project_id = ?')->setParameter(0, $this->currentProject['id'])->andWhere('user_id = ?')->setParameter(1, $user->id)->execute(); } if ($user && isset($member) && $member->rowCount() > 0) { $errors['username'] = $this->translate('errors.users.already_a_project_member'); } // Check if they chose a role if (Request::$post->get('role_id', '') == '') { $errors['role_id'] = $this->translate('errors.validations.required', ['field' => $this->translate('role')]); } // Check if the role exists if (!$role) { $errors['role'] = $this->translate('errors.roles.doesnt_exist'); } // Check if the role belongs to the project if ($role && ($role->project_id != 0 && $role->project_id != $this->currentProject['id'])) { $errors['role'] = $this->translate('errors.roles.invalid_role'); } if (count($errors)) { return $this->render('project_settings/members/new.phtml', ['errors' => $errors]); } else { $userRole = new UserRole(['project_id' => $this->currentProject['id'], 'project_role_id' => $role->id, 'user_id' => $user->id]); $userRole->save(); return $this->redirectTo('project_settings_members'); } }
function createProjectManager($project = null, $user = null) { if (!$project) { $project = createProject(); } if (!$user) { $user = createUser(); } $role = ProjectRole::find(1); $relation = new UserRole(['user_id' => $user['id'], 'project_id' => $project['id'], 'project_role_id' => $role['id']]); $relation->save(); return $relation; }