public function afterSave($insert, $changedAttributes) { if ($insert) { $groupMember = new GroupMember(); $groupMember->group_id = Group::GROUP_USERS; $groupMember->user_id = $this->id; if (!$groupMember->save()) { return false; } } if (parent::afterSave($insert, $changedAttributes)) { return true; } }
public function actionNew() { $request = Yii::$app->request; $user = new User(); if ($request->isPost) { if ($user->load($request->post()) && $user->save()) { $role = (int) $request->post('role'); if (Group::isValidGroup($role)) { $groupMember = new GroupMember(); $groupMember->user_id = $user->id; $groupMember->group_id = $role; if ($groupMember->save()) { return "<a class='auto-close'></a>"; } } else { throw new CHttpException(404, 'Не съществува такава група!'); } } else { return $this->renderPartial('new', ['user' => $user]); } } return $this->renderPartial('new', ['user' => $user]); }
public function processMemberInvite($groupId) { $group = Group::where('id', '=', $groupId)->first(); if (!$group) { return Response::json($this->growlMessage('Invalid Group ID', 'error')); } if (!$group->isGroupOwner(Auth::user()->id)) { return Response::json($this->growlMessage('You cannot add people to a group unless you are the group owner', 'error')); } $email = Input::all()['email']; $role = Input::all()['role']; if (!Group::isValidRole($role)) { return Response::json($this->growlMessage('Invalid role type.', 'error')); } $user = User::where('email', '=', $email)->first(); if (!$user) { return Response::json($this->growlMessage('Invalid user', 'error')); } $userExists = (bool) GroupMember::where('user_id', '=', $user->id)->where('group_id', '=', $group->id)->count(); if ($userExists) { return Response::json($this->growlMessage('This user is already a member of the group!', 'error')); } $newMember = new GroupMember(); $newMember->user_id = $user->id; $newMember->group_id = $group->id; $newMember->role = $role; $newMember->save(); $text = "You've been added to the group " . $group->getDisplayName() . " with the role of " . $role . "."; // Notify member of invite Mail::queue('email.notification', array('text' => $text), function ($message) use($email) { $message->subject("You've been added to a Madison group"); $message->from('*****@*****.**', 'Madison'); $message->to($email); }); return Response::json($this->growlMessage('User added successfully', 'success')); }
public function addMember($userId, $role = null) { $groupMember = $this->findMemberByUserId($userId); if (!$groupMember) { if (is_null($role)) { throw new \Exception("You must provide a role if adding a new member"); } if (!isset($this->id) || empty($this->id)) { throw new \Exception("The group must have a ID set in order to add a member"); } $groupMember = new GroupMember(); $groupMember->user_id = $userId; $groupMember->role = Group::ROLE_OWNER; $groupMember->group_id = $this->id; } else { if (!is_null($role)) { $groupMember->role = $role; } } $groupMember->save(); }