public function handle($request, Closure $next, $classValidate) { $classValidate::boot($request); $validator = Validator::make($request->all(), $classValidate::rules()); if ($validator->fails()) { return response()->json(arrayView('phpsoft.users::errors/validation', ['errors' => $validator->errors()]), 400); } return $next($request); }
public function upload() { $file = Input::file('image'); $validator = $this->validator(['image' => $file]); if ($validator->fails()) { return response()->json(arrayView('phpsoft.photos::errors/validation', ['errors' => $validator->errors()]), 400); } $photo = Photo::upload($file->getPathName()); return response()->json(arrayView('phpsoft.photos::photo/read', ['photo' => $photo]), 201); }
/** * Login action * * @return json */ public function login(Request $request) { // grab credentials from the request $credentials = $request->only('email', 'password'); try { // attempt to verify the credentials and create a token for the user if (!($token = JWTAuth::attempt($credentials))) { return response()->json(arrayView('gcl.gclusers::errors/authenticate', ['error' => 'Invalid Credentials.']), 401); } } catch (JWTException $e) { // something went wrong whilst attempting to encode the token return response()->json(arrayView('gcl.gclusers::errors/authenticate', ['error' => 'Could not create token.']), 500); } // all good so return the token return response()->json(arrayView('gcl.gclusers::tokens/show', compact('token'))); }
public function assignRole($id, Request $request) { $user = AppUser::find($id); if (!$user) { return response()->json(null, 404); } $roleIdOrName = $request->roleIdOrName; $field = is_numeric($roleIdOrName) ? 'id' : 'name'; $role = Role::where($field, $roleIdOrName)->first(); if (!$role) { return response()->json(arrayView('gcl.gclusers::errors/validation', ['errors' => ['Role does not exist.']]), 400); } $hasRole = $user->hasRole($role->name); if ($hasRole) { return response()->json(null, 204); } $user->attachRole($role); return response()->json(null, 204); }
/** * List all routes in app * * @param * @return Response */ public function getAllRoutes() { $routes = Route::getRoutes(); $results = []; if ($routes != null) { foreach ($routes as $route) { $route = array('method' => $route->getMethods(), 'uri' => $route->getPath()); $results[] = (object) $route; } } return response()->json(arrayView('phpsoft.users::route/browse', ['routes' => $results]), 200); }
/** * Update the specified resource in storage. * * @param int $id * @param Request $request * @return Response */ public function update($id, Request $request) { $productModel = $this->productModel; $product = $productModel::find($id); // check exists if (empty($product)) { return response()->json(null, 404); } // validate $validator = Validator::make($request->all(), ['name' => 'string', 'alias' => 'regex:/^[a-z0-9\\-]+/|unique:shop_products,alias,' . $product->id, 'image' => 'string', 'description' => 'string', 'price' => 'numeric', 'galleries' => 'array', 'categories' => 'array', 'attributes' => 'array']); if ($validator->fails()) { return response()->json(arrayView('phpsoft.shoppingcart::errors/validation', ['errors' => $validator->errors()]), 400); } // update $product = $product->update($request->all()); // create/update link between categories and product if ($request->categories) { $product->categories()->sync($request->categories); } // respond return response()->json(arrayView('phpsoft.shoppingcart::product/read', ['product' => $product]), 200); }
/** * Change password * * @param Request $request * @return Response */ public function change(Request $request) { // register validate $this->registerValidators(); if (!$this->checkAuth()) { return response()->json(null, 401); } $validator = Validator::make($request->all(), ['old_password' => 'required|min:6|oldPassword', 'password' => 'required|confirmed|min:6']); if ($validator->fails()) { return response()->json(arrayView('phpsoft.users::errors/validation', ['errors' => $validator->errors()]), 400); } $user = Auth::user(); $change = $user->update(['password' => $request->password]); if (!$change) { return response()->json(null, 500); // @codeCoverageIgnore } return response()->json(null, 204); }
/** * Check role is have all permission action * * @param Request * @return Response */ public function checkAllPerm($id = null) { if (!Role::find($id)) { return response()->json(null, 404); } $permissionRoot = PermissionRole::where(['role_id' => $id, 'permission_id' => 1])->first(); if (!empty($permissionRoot) && $permissionRoot->status == 1) { $isAll = true; } else { $isAll = false; } $roles = ['id' => (int) $id, 'type' => 'permissions', 'isAll' => $isAll]; return response()->json(arrayView('gcl.gclusers::nodePermission/read', ['node' => $roles]), 200); }
/** * Update the specified resource in storage. * * @param int $id * @param Request $request * @return Response */ public function update($id, Request $request) { $categoryModel = $this->categoryModel; $category = $categoryModel::find($id); // check exists if (empty($category)) { return response()->json(null, 404); } // validate $validator = Validator::make($request->all(), ['name' => 'string', 'alias' => 'regex:/^[a-z0-9\\-]+/|unique:shop_categories,alias,' . $category->id, 'image' => 'string', 'description' => 'string', 'parent_id' => 'numeric|not_in:' . $id . ($request->parent_id == 0 || $request->parent_id == null ? '' : '|exists:shop_categories,id'), 'order' => 'numeric', 'status' => 'numeric']); if ($validator->fails()) { return response()->json(arrayView('phpsoft.shoppingcart::errors/validation', ['errors' => $validator->errors()]), 400); } // update $category = $category->update($request->all()); // respond return response()->json(arrayView('phpsoft.shoppingcart::category/read', ['category' => $category]), 200); }
/** * List all routes in app has not been added to permissions tree * * @param * @return Response */ public function getAllRoutesNotTree() { // Get all routes $routes = Route::getRoutes(); // Get all routes has been added to permissions tree $permissionOnTree = PermissionRoute::all()->toArray(); $diff = (new PermissionRoute())->getRouteNotTree($routes, $permissionOnTree); return response()->json(arrayView('gcl.gclusers::route/browse', ['routes' => $diff]), 200); }
/** * index * @return json */ public function index(Request $request) { $permissions = Permission::browse(['order' => [Input::get('sort', 'id') => Input::get('direction', 'desc')], 'limit' => $limit = (int) Input::get('limit', 25), 'offset' => (Input::get('page', 1) - 1) * $limit, 'filters' => $request->all()]); return response()->json(arrayView('phpsoft.users::permission/browse', ['permissions' => $permissions]), 200); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $categoryModel = $this->categoryModel; // retrieve category $category = $categoryModel::withTrashed()->where('id', $id)->first(); // check exists if (empty($category)) { return response()->json(null, 404); } // check reference article $articleModel = config('phpsoft.article.articleModel'); $hasArticles = $articleModel::where('category_id', $id)->count(); if ($hasArticles) { return response()->json(arrayView('phpsoft.articles::errors/validation', ['errors' => ['Can not delete this category. You must to delete article before deleting category.']]), 403); } $category->forceDelete(); return response()->json(null, 204); }
/** * index * @param int $id * @return json */ public function indexByUser($id) { $user = \App\User::find($id); if (!$user) { return response()->json(null, 404); } $roles = Role::browseByUser(['order' => [Input::get('sort', 'name') => Input::get('direction', 'asc')], 'limit' => $limit = (int) Input::get('limit', 25), 'offset' => (Input::get('page', 1) - 1) * $limit, 'user' => $user]); return response()->json(arrayView('phpsoft.users::role/browse', ['roles' => $roles]), 200); }
/** * Update the specified resource in storage. * * @param int $id * @param Request $request * @return Response */ public function update($id, Request $request) { // check authenticate if (!$this->checkAuth()) { return response()->json(null, 401); } // check permission if (!$this->checkPermission('edit-product')) { return response()->json(null, 403); } $product = Product::find($id); // check exists if (empty($product)) { return response()->json(null, 404); } // validate $validator = Validator::make($request->all(), ['title' => 'string', 'alias' => 'regex:/^[a-z0-9\\-]+/|unique:shop_products,alias,' . $product->id, 'image' => 'string', 'description' => 'string', 'price' => 'numeric', 'galleries' => 'array']); if ($validator->fails()) { return response()->json(arrayView('errors/validation', ['errors' => $validator->errors()]), 400); } // update $product = $product->update($request->all()); // respond return response()->json(arrayView('product/read', ['product' => $product]), 200); }
/** * @expectedException BadFunctionCallException * @expectedExceptionMessage Helper [helperInvalid] is invalid. */ public function testHelperInvalid() { $results = arrayView('testHelper/testHelperInvalid', array('title' => 'example title')); }
/** * Fire event and return the response * * @param string $event * @param string $error * @param integer $status * @param array $payload * @return mixed */ protected function respond($event, $error, $status, $payload = []) { $response = $this->events->fire($event, $payload, true); return $response ?: $this->response->json(arrayView('phpsoft.users::errors/authenticate', ['error' => $error]), $status); }
/** * Display a listing of the resource. * * @return Response */ public function index(Request $request) { $isTrash = $request->is('articles/trash'); $articleModel = $this->articleModel; $articles = $articleModel::browse(['order' => [Input::get('sort', 'order') => Input::get('direction', 'asc')], 'limit' => $limit = (int) Input::get('limit', 25), 'offset' => (Input::get('page', 1) - 1) * $limit, 'cursor' => Input::get('cursor'), 'filters' => $request->all(), 'trash' => $isTrash]); return response()->json(arrayView('phpsoft.articles::article/browse', ['articles' => $articles]), 200); }
/** * Display a listing of the resource. * * @return Response */ public function index($url) { $comments = Comment::browse(['limit' => $limit = (int) Input::get('limit', 25), 'cursor' => Input::get('cursor'), 'offset' => (Input::get('page', 1) - 1) * $limit, 'url' => $url]); return response()->json(arrayView('phpsoft.comments::comment/browse', ['comments' => $comments]), 200); }