/** * Override setting the user_id attribute to automatically adjust user roles. * @param $value */ public function setUserIdAttribute($value) { // Only process the change in permissions // if the assigned user has changed $old_id = $this->getAttributeValue('user_id'); if ($old_id != $value) { // Don't allow self-unassignment if ($old_id == Auth::user()->id) { Flash::warning('You can\'t remove yourself from the committee'); return; } // Get the necessary roles $committee = Role::where('name', 'committee')->first(); $member = Role::where('name', 'member')->first(); // Look through the database for any other committee roles for // the old user. If they exist then we don't want to remove // their committee permissions. $old_user = User::find($old_id); if ($old_user && $old_user->hasRole($committee->name)) { if (CommitteeRole::where('user_id', '=', $old_user->id)->where('id', '<>', $this->id)->get()->count() == 0) { $old_user->detachRole($committee); $old_user->attachRole($member); } } // Always give the new user committee permissions $new_user = User::find($value); if ($new_user && !$new_user->hasRole($committee->name)) { $new_user->attachRole($committee); $new_user->detachRole($member); } } // Set the new id $this->attributes['user_id'] = $value; }
/** * @param int $order * @return int */ private function verifyRoleOrder($order) { if ($order < 1 || $order > CommitteeRole::count() && $order != 1) { $order = CommitteeRole::count() + 1; return $order; } return $order; }
/** * Determine the positions available in an election. * @param \App\Http\Requests\ElectionRequest $request * @return array */ private function determineElectionPositions(ElectionRequest $request) { if ($request->get('type') == 2) { $positions_checked = $request->get('positions_checked'); $positions = array_values(array_filter($request->get('positions'), function ($index) use($positions_checked) { return in_array($index, $positions_checked); }, ARRAY_FILTER_USE_KEY)); } else { $positions = CommitteeRole::orderBy('order', 'ASC')->lists('name', 'id')->toArray(); } return $positions; }