/** * Execute the console command. * * @return mixed */ public function handle() { $this->call('migrate'); $role = Role::create(['name' => 'admin', 'display_name' => '超级管理员']); $permission = Permission::create(['name' => 'admin', 'display_name' => '超级管理员权限']); $user = User::create(['name' => '超级管理员', 'email' => env('ADMIN_EMAIL', '*****@*****.**'), 'password' => bcrypt(env('ADMIN_PASSWORD', 'admin'))]); $role->attachPermission($permission); $user->attachRole($role); $this->info('Forone initialized!'); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, UpdateAdminRequest $request) { $name = $request->get('name'); $email = $request->get('email'); $password = $request->get('new_password'); $count = User::whereName($name)->where('id', '!=', $id)->count(); if ($count > 0) { return $this->redirectWithError('名称不能重复'); } $count = User::whereEmail($email)->where('id', '!=', $id)->count(); if ($count > 0) { return $this->redirectWithError('邮箱不能重复'); } if (!empty($password)) { User::findOrFail($id)->update(array('name' => $name, 'email' => $email, 'password' => bcrypt($password))); \Auth::logout(); return redirect('/admin/auth/login')->withErrors(array('default' => '密码重置成功,请重新登录')); } else { User::findOrFail($id)->update($request->only(['name', 'email'])); return redirect()->route('account.index')->withErrors(array('default' => '编辑成功')); } }
/** * Delete the specified resource in storage * @param $id * @return \Illuminate\Http\RedirectResponse */ public function destroy($id) { if (User::destroy($id)) { return $this->toIndex('删除成功'); } else { return $this->toIndex('删除失败'); } }
/** * 分配角色 */ public function assignRole(Request $request) { $user = User::find($request->get('id')); $roles = $request->except(['_token', 'id']); $user->detachRoles($user->roles()->get()); foreach ($roles as $name => $status) { $role = Role::whereName($name)->first(); if ($status == 'on') { $user->attachRole($role); } } return $this->toIndex('角色分配成功'); }
/** * Create a new user instance after a valid registration. * * @param array $data * @return User */ public function create(array $data) { return User::create(['name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password'])]); }