public function update($id, array $attributes)
 {
     try {
         // Find the user using the user id
         $user = Sentry::findUserById($id);
         $group_id = 0;
         foreach ($user->groups as $group) {
             $group_id = $group->id;
         }
         if ($group_id > 0 && $group_id != $attributes['group_id']) {
             // User is  Assigned , So Remove Old Group and Re-assigned New Group
             // Find Old the group using the group id
             $oldGroup = Sentry::findGroupById($group_id);
             $user->removeGroup($oldGroup);
             // Find New the group using the group id
             $newGroup = Sentry::findGroupById($attributes['group_id']);
             $user->addGroup($newGroup);
         } else {
             if ($group_id == 0 && $attributes['group_id'] > 0) {
                 // User is Not Assigned , So Assigned New Group
                 // Find New the group using the group id
                 $newGroup = Sentry::findGroupById($attributes['group_id']);
                 $user->addGroup($newGroup);
             }
         }
         $user->email = $attributes['email'];
         $user->save();
         return true;
     } catch (\Exception $e) {
         return false;
     }
 }
Example #2
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $data = array("email" => Input::get("email"), "first_name" => Input::get("first_name"), "last_name" => Input::get("last_name"), "password" => Input::get("password"), "repassword" => Input::get("repassword"), "groups" => Input::get("groups") == null ? array() : Input::get("groups"));
     $rules = array("email" => "required|unique:users,email", "first_name" => "required", "last_name" => "required", "password" => "required|min:6", "repassword" => "required|min:6|same:password");
     $validator = Validator::make($data, $rules);
     if ($validator->fails()) {
         return Redirect::route('admin.users.create')->withErrors($validator)->withInput();
     } else {
         $user = new AdminUser();
         $user->email = $data['email'];
         $user->first_name = $data['first_name'];
         $user->last_name = $data['last_name'];
         $user->password = Hash::make($data['password']);
         if ($user->save()) {
             $s_user = Sentry::findUserById($user->id);
             foreach ($data["groups"] as $group) {
                 $s_user->addGroup(Sentry::findGroupById($group));
             }
             Session::flash('success', "Đã thêm user" . $data['email'] . " thành công");
             return Redirect::route('admin.users.create');
         } else {
             Session::flash('error', "Xảy ra lỗi trong khi thêm user " . $data['name']);
             return Redirect::route('admin.users.create');
         }
     }
 }
Example #3
0
 /**
  * @return \Illuminate\Http\JsonResponse
  */
 public function postStore()
 {
     $user = Sentry::createUser(Input::all());
     //添加默认用户组
     $genealGroup = Sentry::findGroupById(2);
     $user->addGroup($genealGroup);
     if ($user) {
         return Response::json(['status' => 1]);
     } else {
         return Response::json(['status' => 0]);
     }
 }
Example #4
0
 /**
  * Deletes specified group.
  * 
  * @param  array $id
  */
 public function delete($id)
 {
     try {
         $group = Sentry::findGroupById($id);
         $group->where('id', $id)->forceDelete();
         \Cache::flush();
         Event::fire('Groups.Deleted', array($id, Carbon::now(), 'Deleted'));
         return 'success';
     } catch (GroupNotFoundException $e) {
         return 'Group was not found.';
     }
 }
 public function run()
 {
     User::truncate();
     $faker = Faker\Factory::create();
     for ($i = 0; $i < 10; $i++) {
         try {
             // Create the user
             $user = Sentry::createUser(array('first_name' => $faker->name, 'last_name' => $faker->name, 'email' => $faker->unique()->email, 'phone' => $faker->unique()->phoneNumber, 'image_url' => $faker->imageUrl($width = 640, $height = 480), 'password' => '123', 'activated' => true));
             // Find the group using the group id
             $adminGroup = Sentry::findGroupById($faker->randomElement(array(1, 2, 3)));
             // Assign the group to the user
             $user->addGroup($adminGroup);
         } catch (\Exception $e) {
         }
     }
 }
Example #6
0
 /**
  * 添加用户
  * @return mixed
  */
 public function store()
 {
     $user['email'] = $user['username'] = Input::get('email');
     $user['password'] = Input::get('password0');
     $mail_url = (new Uinfo())->getMail($user['email']);
     //判断用户是否存在
     $user_exist = User::where('email', $user['email'])->first();
     if ($user_exist) {
         return Redirect::back()->withErrors('邮箱已被占用!');
     }
     $errors = '';
     //添加用户
     try {
         $user = Sentry::register($user);
         //添加默认用户组
         $genealGroup = Sentry::findGroupById(2);
         $user->addGroup($genealGroup);
         //添加用户对应信息
         Uinfo::create(['uid' => $user->id]);
         //登陆该用户
         $data['activationCode'] = $user->getActivationCode();
         $data['uid'] = $user->id;
         Mail::send('emails.auth.active', $data, function ($message) use($user) {
             $message->to($user->email, '尊敬的柚皮会员')->subject('欢迎注册柚皮网');
         });
     } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
         $errors = '请输入完整注册信息.';
     } catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
         $errors = '请输入密码.';
     } catch (Cartalyst\Sentry\Users\UserExistsException $e) {
         $errors = '用户已存在.';
     }
     if ($errors) {
         return Redirect::back()->withErrors($errors);
     } else {
         return View::make('auth.active')->withEmail($mail_url);
     }
 }
Example #7
0
 public function setGroupsAttribute($groups = array())
 {
     // Remove exisiting groups
     foreach ($this->groups as $group) {
         $this->removeGroup($group);
     }
     // Add new groups
     foreach ($groups as $id) {
         $group = Sentry::findGroupById($id);
         $this->addGroup($group);
     }
 }
 /**
  * 用户组添加成员
  * @param $id
  * @return \Illuminate\Http\JsonResponse
  */
 public function putAddMemberToGroup($id)
 {
     $group = Sentry::findGroupById($id);
     $errors = '';
     foreach (Input::all() as $user) {
         if ($user['checked']) {
             $member = Sentry::findUserById($user['id']);
             if (!$member->addGroup($group)) {
                 $errors = true;
             }
         }
     }
     return Response::json(['status' => $errors ? 0 : 1]);
 }
Example #9
0
 public function postUsers(Request $request)
 {
     $outputMessage = array();
     if ($request->has('action')) {
         switch ($request->get('action')) {
             case 'new':
                 $email = $request->get('email');
                 $password = $request->get('password');
                 $firstname = $request->get('firstname');
                 $lastname = $request->get('lastname');
                 //$groupName = $request->get('group');
                 $groupID = $request->get('group');
                 try {
                     $user = Sentry::register(array('email' => $email, 'first_name' => $firstname, 'last_name' => $lastname, 'password' => $password, 'activated' => true));
                     if ($user) {
                         $outputMessage[] = array("type" => "success", "msg" => "User created successfuly.");
                         if ($groupID == 1) {
                             $group = Sentry::findGroupByName('user');
                             $user->addGroup($group);
                             $outputMessage[] = array("type" => "alert", "msg" => "The God is unique.User is added to User group ;)");
                         } else {
                             $group = Sentry::findGroupById($groupID);
                             if ($group) {
                                 $user->addGroup($group);
                                 $outputMessage[] = array("type" => "success", "msg" => "User added to " . $group->name . " successfuly");
                             } else {
                                 $group = Sentry::findGroupByName('user');
                                 $user->addGroup($group);
                                 $outputMessage[] = array("type" => "alert", "msg" => "Group " . $group->name . " not found.User is added to User group ;)");
                             }
                         }
                     }
                 } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "Login field is required.");
                 } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "Login field is required.");
                 } catch (\Cartalyst\Sentry\Users\UserExistsException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "User with this login already exists.");
                 } catch (\Cartalyst\Sentry\Groups\GroupNotFoundException $e) {
                     $outputMessage[] = array("type" => "alert", "msg" => "Group was not found.");
                 }
                 break;
         }
     }
     $groups = Sentry::findAllGroups();
     $users = User::select(array('id', 'first_name', 'last_name', 'email'))->paginate(20);
     return view('admin.users')->with('users', $users)->with('groups', $groups)->with('messages', $outputMessage);
 }
Example #10
0
 /**
  * Delete found group
  *
  * @param int $id
  * @return bool
  */
 public function delete($id)
 {
     try {
         $group = Sentry::findGroupById($id);
         $group->delete();
     } catch (GroupNotFoundException $e) {
         return false;
     }
     return true;
 }
 public function deletepermission()
 {
     $input = Input::all();
     $objPermission = new Permission();
     foreach ($input['aids'] as $v) {
         $objPermission->destroy($v);
     }
     $permissions = $objPermission->get();
     $listkey = array();
     foreach ($permissions as $v) {
         $listkey[] = $v['key'];
     }
     $objGroup = new Groups();
     $groups = $objGroup->get();
     foreach ($groups as $v) {
         $group = Sentry::findGroupById($v['id']);
         $groupPermissions = $group->getPermissions();
         foreach ($groupPermissions as $k => $vl) {
             if (!in_array($k, $listkey)) {
                 $groupPermissions[$k] = 0;
             }
         }
         $group->permissions = $groupPermissions;
         $group->save();
     }
     return Response::json('1');
 }
Example #12
0
 /**
  * Checks if a user belongs to the given Role ID
  * @param  int  $roleId
  * @return bool
  */
 public function hasRoleId($roleId)
 {
     $role = Sentry::findGroupById($roleId);
     return $this->inGroup($role);
 }
 public function store()
 {
     $input = Input::all();
     Input::flash();
     if (isset($input['id']) && $input['id'] != '') {
         $input = Input::all();
         $rules = array('id' => array('required'), 'email' => 'required|email');
         $useredit = Sentry::findUserById($input['id']);
         if ($input['password'] != '') {
             $rules['password'] = array('required', 'min:6');
             $rules['retype_password'] = array('required', 'min:6');
         }
         $validation = Validator::make(Input::all(), $rules);
         //check email
         if ($validation->fails()) {
             $message = '';
             $mess = $validation->errors()->getMessages();
             foreach ($mess as $v) {
                 $message .= $v[0] . ' ';
             }
             if ($message != '') {
                 Session::flash('message', $message);
             }
             if (!isset($input['id'])) {
                 return Redirect::to('manager/users');
             }
             return Redirect::to('manager/users/edit/' . $input['id']);
         } else {
             try {
                 $useredit->email = $input['email'];
                 $useredit->first_name = isset($input['first_name']) ? $input['first_name'] : '';
                 $useredit->last_name = isset($input['last_name']) ? $input['last_name'] : '';
                 if ($input['password'] != '') {
                     $useredit->password = $input['password'];
                 }
                 if ($useredit->save()) {
                     $groupscursor = $useredit->getGroups();
                     $groups = array();
                     $inputgroups = isset($input['groups']) ? $input['groups'] : array();
                     foreach ($groupscursor as $v) {
                         if (!in_array($v['id'], $inputgroups)) {
                             $g = Sentry::findGroupById($v['id']);
                             $useredit->removeGroup($g);
                         }
                         $groups[] = $v['id'];
                     }
                     if (isset($input['groups'])) {
                         foreach ($input['groups'] as $v) {
                             if (!in_array($v, $groups)) {
                                 $Group = Sentry::findGroupById($v);
                                 $useredit->addGroup($Group);
                             }
                         }
                     }
                 }
                 Session::flash('message', trans('saved'));
                 return Redirect::to('manager/users');
             } catch (LoginRequiredException $e) {
                 Session::flash('message', 'Login field is required.');
                 return Redirect::to('manager/users/edit/' . $input['id']);
             } catch (PasswordRequiredException $e) {
                 Session::flash('message', 'Password field is required.');
                 return Redirect::to('manager/users/edit/' . $input['id']);
             } catch (UserExistsException $e) {
                 Session::flash('message', 'User with this login already exists.');
                 return Redirect::to('manager/users/edit/' . $input['id']);
             } catch (GroupNotFoundException $e) {
                 Session::flash('message', 'Group was not found.');
                 return Redirect::to('manager/users/edit/' . $input['id']);
             }
         }
     } else {
         $rules = array('password' => array('required', 'min:6'), 'retype_password' => array('required', 'min:6'), 'email' => 'required|email');
         $validation = Validator::make(Input::all(), $rules);
         //check
         $objUsers = new Users();
         $check = $objUsers->where('email', '=', trim(strtolower($input['email'])))->count();
         if ($validation->fails() || $check > 0) {
             $messages = '';
             if ($check > 0) {
                 $messages .= 'User with this email already exists. ';
             }
             $mess = $validation->errors()->getMessages();
             foreach ($mess as $v) {
                 $messages .= $v[0] . ' ';
             }
             if ($messages != '') {
                 Session::flash('message', $messages);
             }
             return Redirect::to('manager/users/edit');
         } else {
             try {
                 $datacreateUser = array('email' => trim(strtolower($input['email'])), 'password' => isset($input['password']) ? $input['password'] : '', 'activated' => true, 'first_name' => isset($input['first_name']) ? $input['first_name'] : '', 'last_name' => isset($input['last_name']) ? $input['last_name'] : '');
                 $user = Sentry::createUser($datacreateUser);
                 /*$email = trim(strtolower($input['email']));
                   $first_name = trim(strtolower($input['first_name']));*/
                 if (isset($input['groups']) && isset($user['id'])) {
                     foreach ($input['groups'] as $v) {
                         $Group = Sentry::findGroupById($v);
                         $user->addGroup($Group);
                     }
                 }
                 /*Mail::send('system::email.email_template.acount_info', $input, function($message) use ($email, $first_name) {
                       $message->to($email, $first_name)->subject('Welcome to the Tastable.net');
                   });*/
             } catch (LoginRequiredException $e) {
                 Session::flash('message', 'Login field is required. ');
                 return Redirect::to('manager/users/edit');
             } catch (PasswordRequiredException $e) {
                 Session::flash('message', 'Password field is required. ');
                 return Redirect::to('manager/users/edit');
             } catch (UserExistsException $e) {
                 Session::flash('message', 'User with this email already exists. ');
                 return Redirect::to('manager/users/edit');
             } catch (GroupNotFoundException $e) {
                 Session::flash('message', 'Group was not found.');
                 return Redirect::to('manager/users/edit');
             }
         }
         Session::flash('message', 'User created');
         return Redirect::to('manager/users');
     }
 }
Example #14
0
 /**
  * Update a user and sync its roles
  * @param  int   $userId
  * @param $data
  * @param $roles
  * @return mixed
  */
 public function updateAndSyncRoles($userId, $data, $roles)
 {
     $user = Sentry::findUserById($userId);
     $this->checkForNewPassword($data);
     $user->update($data);
     if (!empty($roles)) {
         // Get the user roles
         $userRoles = $user->groups()->get()->toArray();
         $cleanedRoles = [];
         foreach ($userRoles as $role) {
             $cleanedRoles[$role['id']] = $role;
         }
         // Set the new roles
         foreach ($roles as $roleId) {
             if (isset($cleanedRoles[$roleId])) {
                 unset($cleanedRoles[$roleId]);
             }
             $group = Sentry::findGroupById($roleId);
             $user->addGroup($group);
         }
         // Unset the unchecked roles
         foreach ($cleanedRoles as $roleId => $role) {
             $group = Sentry::findGroupById($roleId);
             $user->removeGroup($group);
         }
     }
 }
Example #15
0
 /**
  * Delete a role
  * @param $id
  * @return mixed
  */
 public function delete($id)
 {
     $role = Sentry::findGroupById($id);
     return $role->delete();
 }
Example #16
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     $data = array("title" => Input::get("title"), "name" => Input::get("alias"), "permissions" => Input::get("permissions"));
     $rules = array("title" => "max:255", "name" => array("required", "max:40", "regex:/^([a-zA-Z]|\\.|_|-)+\$/", "unique:groups,name," . $id));
     $validator = Validator::make($data, $rules);
     if ($validator->fails()) {
         return Redirect::route('admin.groups.edit')->withErrors($validator)->withInput();
     } else {
         try {
             // Find the group using the group id
             $group = Sentry::findGroupById($id);
             $group->title = $data['title'];
             $group->name = $data['name'];
             // Update the group details
             $group->permissions = $data['permissions'] == null ? "" : $data['permissions'];
             // Update the group
             if ($group->save()) {
                 Session::flash('success', "Đã cập nhật module " . $data['name'] . " thành công");
                 return Redirect::route('admin.groups.edit', $id);
             } else {
                 Session::flash('error', "Xảy ra lỗi không thể cập nhật được nhóm " . $data['name']);
                 return Redirect::route('admin.groups.edit', $id);
             }
         } catch (Cartalyst\Sentry\Groups\GroupExistsException $e) {
             Session::flash('error', "Nhóm đã tồn tại " . $data['name']);
             return Redirect::route('admin.groups.edit', $id);
         } catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e) {
             Session::flash('error', "Không tìm thấy nhóm " . $data['name']);
             return Redirect::route('admin.groups.edit', $id);
         }
     }
 }