protected function beforeSave(&$Item, $id, &$item, &$request, $func) { if (json_decode($request->content) == false) { throw new Exception('decode'); } parent::beforeSave($Item, $id, $item, $request, $func); }
protected function beforeSave(&$Item, $id, &$item, &$request, $func) { $preserved_data = ['name' => $item->name, 'content' => $item->content]; parent::beforeSave($Item, $id, $item, $request, $func); $preserved = false; //check if the uploaded file is valid if (!$request->hasFile('content') || !$request->file('content')->isValid()) { if (isset($_FILES['content'])) { if ($_FILES['content']['error'] >= 1 && $_FILES['content']['error'] <= 4) { if ($func == 'update' && $_FILES['content']['error'] == 4) { $preserved = true; } else { throw new Exception('upload' . $_FILES['content']['error']); } } else { $this->exceptionAdd(['upload' => 'Upload file error! [' . $_FILES['content']['error'] . ']']); throw new Exception('upload'); } } else { $this->exceptionAdd(['upload' => 'Upload file error! [Unknown]']); throw new Exception('upload'); } } if ($preserved == true) { if (!isset($request['name']) || $request['name'] == '') { $item->name = $preserved_data['name']; } $item->content = $preserved_data['content']; } else { if ($_FILES['content']['size'] > $this->maxFileSize) { throw new Exception('upload2'); } $file = $request->file('content'); //pre-filesystem //generate name automatically if (!isset($request['name']) || $request['name'] == '') { $item->name = $file->getClientOriginalName(); } //generate path $path = date('Y-m-d-H-i-s-') . uniqid(); $this->encodeFileContent($item, $file->getClientOriginalName(), $path); //filesystem //delete old file if ($func == 'update') { $old_item = File::find($id); $this->decodeFileContent($old_item); $old_path = $old_item->filepath; if (file_exists($this->desRoot . $old_path) && !@unlink($this->desRoot . $old_path)) { throw new Exception('delete'); } } //move new file try { $file->move($this->desRoot, $path); } catch (Exception $ex) { throw new Exception('move'); } } }
protected function beforeSave(&$Item, $id, &$item, &$request, $func) { try { Category::checkJson($request->content); } catch (Exception $ex) { $appendMsg = ['json' => $ex->getMessage()]; $this->exceptionAdd($appendMsg); throw new Exception('json'); } parent::beforeSave($Item, $id, $item, $request, $func); }
protected function beforeSave(&$Item, $id, &$item, &$request, $func) { $user = auth()->user(); if ($user->id != 0 && $id == 0) { //only SuperUser [ID:0] can update itself. throw new Exception('protected_user'); } if ($func == 'update' && $id == 0 && ($request->id != 0 || $request->UAL != 0 || $request->category != 'Root')) { throw new Exception('protected_user'); } if ($id == 0) { $this->CategoryWithUAM = false; $this->CheckIfUserPassCategory = false; //$originalCategory = $item->category; } parent::beforeSave($Item, $id, $item, $request, $func); unset($item->creator); unset($item->owner); unset($item->content); $item->email = $request->email; if ($func == 'store' && strlen($request->password) < $this->password_min) { //new user throw new Exception('password'); } else { if (strlen($request->password) >= $this->password_min) { //modify existed user $item->password = bcrypt($request->password); } } $item->creator = $request->id; //protected_user if ($id == 0) { $this->CategoryWithUAM = true; $this->CheckIfUserPassCategory = true; //$item->category = $originalCategory; } }