public function run() { DB::table('roles')->delete(); $adminRole = new Role(); $adminRole->name = 'admin'; $adminRole->display_name = 'admin'; $adminRole->description = 'description'; $adminRole->is_admin = 1; $adminRole->save(); $commentRole = new Role(); $commentRole->name = 'comment'; $commentRole->display_name = 'comment'; $commentRole->description = 'description'; $commentRole->is_admin = 0; $commentRole->save(); $user = User::where('email', '=', '*****@*****.**')->first(); $assignedrole = new AssignedRoles(); $assignedrole->user_id = $user->id; $assignedrole->role_id = $adminRole->id; $assignedrole->save(); $user = User::where('email', '=', '*****@*****.**')->first(); $assignedrole = new AssignedRoles(); $assignedrole->user_id = $user->id; $assignedrole->role_id = $commentRole->id; $assignedrole->save(); }
/** * Initializer. * * @access public * @return \BaseController */ public function __construct() { $this->beforeFilter('csrf', array('on' => 'post')); $user = Auth::id(); if ($user > 0) { $result = IsAdminRoles::join('permission_role', 'assigned_roles.role_id', '=', 'permission_role.role_id')->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')->where('assigned_roles.user_id', $user)->select('name')->get(); foreach ($result as $row) { View::share($row->name, $row->name); } $count = IsAdminRoles::join('permission_role', 'assigned_roles.role_id', '=', 'permission_role.role_id')->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')->where('assigned_roles.user_id', $user)->where('permissions.is_admin', '1')->count(); if ($count > 0) { View::share('admin', 'admin'); } } }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($this->auth->check()) { $admin = 0; $user_roles = AssignedRoles::join('roles', 'role_user.role_id', '=', 'roles.id')->where('user_id', $this->auth->user()->id)->select('roles.is_admin')->get(); foreach ($user_roles as $item) { if ($item->is_admin == 1) { $admin = 1; } } if ($admin == 0) { return $this->response->redirectTo('/'); } return $next($request); } return $this->response->redirectTo('/'); }
/** * Update the specified resource in storage. * * @param $user * @return Response */ public function postEdit(UserEditRequest $request, $id) { $user = User::find($id); $user->name = $request->name; $user->confirmed = $request->confirmed; $password = $request->password; $passwordConfirmation = $request->password_confirmation; if (!empty($password)) { if ($password === $passwordConfirmation) { $user->password = bcrypt($password); } } $user->save(); AssignedRoles::where('user_id', '=', $user->id)->delete(); foreach ($request->roles as $item) { $role = new AssignedRoles(); $role->role_id = $item; $role->user_id = $user->id; $role->save(); } return redirect('admin/users')->with('success', 'User has been updated.'); }
/** * Handle a login request to the application. * * @Post("auth/login") * * @param LoginRequest $request * @return Response */ public function login(LoginRequest $request) { if ($this->auth->attempt($request->only('email', 'password'))) { $count = IsAdminRoles::join('permission_role', 'assigned_roles.role_id', '=', 'permission_role.role_id')->join('permissions', 'permissions.id', '=', 'permission_role.permission_id')->where('assigned_roles.user_id', $this->auth->id())->where('permissions.is_admin', '1')->count(); if ($count > 0) { return redirect('admin/dashboard'); } return redirect('/'); } return redirect('/auth/login')->withErrors(['email' => 'These credentials do not match our records.']); }
/** * Update the specified resource in storage. * * @param $user * @return Response */ public function postEdit(UserEditRequest $request, $id) { $user = User::find($id); $user->name = $request->name; $user->confirmed = $request->confirmed; $password = $request->password; $passwordConfirmation = $request->password_confirmation; if (!empty($password)) { if ($password === $passwordConfirmation) { $user->password = Hash::make($password); } } $user->save(); AssignedRoles::where('user_id', '=', $user->id)->delete(); foreach ($request->roles as $item) { $role = new AssignedRoles(); $role->role_id = $item; $role->user_id = $user->id; $role->save(); } }