Skip to content

Commit

Permalink
Merge pull request #282 from uasoft-indonesia/bugfix/v1/fix-various-bug
Browse files Browse the repository at this point in the history
Fix validation
  • Loading branch information
rizkiheryandi authored Jul 22, 2021
2 parents a1c0936 + 8a15472 commit 6019799
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 66 deletions.
21 changes: 12 additions & 9 deletions src/Controllers/BadasoAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Uasoft\Badaso\Models\User;
use Uasoft\Badaso\Models\UserRole;
use Uasoft\Badaso\Models\UserVerification;
use Uasoft\Badaso\Rules\ExistsModel;
use Uasoft\Badaso\Rules\UniqueModel;
use Uasoft\Badaso\Traits\FileHandler;

class BadasoAuthController extends Controller
Expand Down Expand Up @@ -90,9 +92,10 @@ public function register(Request $request)
{
try {
DB::beginTransaction();
// new ExistsModel(User::class, 'email')
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:Uasoft\Badaso\Models\User',
'email' => ['required', 'string', 'email', 'max:255', new UniqueModel(User::class, 'email')],
'password' => 'required|string|min:6|confirmed',
]);

Expand Down Expand Up @@ -194,7 +197,7 @@ public function verify(Request $request)
{
try {
$request->validate([
'email' => ['required', 'exists:Uasoft\Badaso\Models\User'],
'email' => ['required', new ExistsModel(User::class, 'email')],
'token' => ['required'],
]);

Expand Down Expand Up @@ -268,7 +271,7 @@ public function forgetPassword(Request $request)
{
try {
$request->validate([
'email' => ['required', 'email', 'exists:Uasoft\Badaso\Models\User,email'],
'email' => ['required', 'email', new ExistsModel(User::class, 'email')],
]);

$token = rand(111111, 999999);
Expand All @@ -292,10 +295,10 @@ public function validateTokenForgetPassword(Request $request)
{
try {
$request->validate([
'email' => ['required', 'email', 'exists:Uasoft\Badaso\Models\User,email'],
'email' => ['required', 'email', new ExistsModel(User::class, 'email')],
'token' => [
'required',
'exists:Uasoft\Badaso\Models\PasswordReset,token',
new ExistsModel(PasswordReset::class, 'token'),
function ($attribute, $value, $fail) use ($request) {
$password_resets = PasswordReset::where('token', $request->token)->where('email', $request->email)->get();
$password_reset = collect($password_resets)->first();
Expand All @@ -316,10 +319,10 @@ public function resetPassword(Request $request)
{
try {
$request->validate([
'email' => ['required', 'email', 'exists:Uasoft\Badaso\Models\User,email'],
'email' => ['required', 'email', new ExistsModel(User::class, 'email')],
'token' => [
'required',
'exists:Uasoft\Badaso\Models\PasswordReset,token',
new ExistsModel(PasswordReset::class, 'token'),
function ($attribute, $value, $fail) use ($request) {
$password_resets = PasswordReset::where('token', $request->token)->where('email', $request->email)->get();
$password_reset = collect($password_resets)->first();
Expand Down Expand Up @@ -451,7 +454,7 @@ public function updateEmail(Request $request)
}

$request->validate([
'email' => 'required|email|unique:Uasoft\Badaso\Models\User,email',
'email' => ['required', 'email', new UniqueModel(User::class, 'email')],
]);

$user = User::find($user->id);
Expand Down Expand Up @@ -507,7 +510,7 @@ public function verifyEmail(Request $request)
}

$request->validate([
'email' => ['required', 'unique:Uasoft\Badaso\Models\User', 'email'],
'email' => ['required', new UniqueModel(User::class, 'email'), 'email'],
'token' => ['required'],
]);

Expand Down
3 changes: 2 additions & 1 deletion src/Controllers/BadasoBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Uasoft\Badaso\Helpers\Firebase\FCMNotification;
use Uasoft\Badaso\Helpers\GetData;
use Uasoft\Badaso\Models\DataType;
use Uasoft\Badaso\Rules\ExistsModel;

class BadasoBaseController extends Controller
{
Expand Down Expand Up @@ -368,7 +369,7 @@ public function setMaintenanceState(Request $request)
DB::beginTransaction();
try {
$request->validate([
'slug' => 'required|exists:Uasoft\Badaso\Models\DataType,slug',
'slug' => ['required', new ExistsModel(DataType::class, 'slug')],
'is_maintenance' => 'required',
]);

Expand Down
14 changes: 8 additions & 6 deletions src/Controllers/BadasoCRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Uasoft\Badaso\Models\Menu;
use Uasoft\Badaso\Models\MenuItem;
use Uasoft\Badaso\Models\Permission;
use Uasoft\Badaso\Rules\ExistsModel;
use Uasoft\Badaso\Rules\UniqueModel;

class BadasoCRUDController extends Controller
{
Expand Down Expand Up @@ -61,7 +63,7 @@ public function read(Request $request)
{
try {
$request->validate([
'table' => 'required|exists:Uasoft\Badaso\Models\DataType,name',
'table' => ['required', new ExistsModel(DataType::class, 'name')],
]);
$table = $request->input('table', '');
$data_type = Badaso::model('DataType')::where('name', $table)->first();
Expand Down Expand Up @@ -105,7 +107,7 @@ public function readBySlug(Request $request)
{
try {
$request->validate([
'slug' => 'required|exists:Uasoft\Badaso\Models\DataType,slug',
'slug' => ['required', new ExistsModel(DataType::class, 'slug')],
]);
$slug = $request->input('slug', '');
$data_type = Badaso::model('DataType')::where('slug', $slug)->first();
Expand All @@ -128,10 +130,10 @@ public function edit(Request $request)

try {
$request->validate([
'id' => 'required|exists:Uasoft\Badaso\Models\DataType',
'id' => ['required', new ExistsModel(DataType::class)],
'name' => [
'required',
"unique:Uasoft\Badaso\Models\DataType,name,{$request->id}",
new UniqueModel(DataType::class, 'name', $request->id),
function ($attribute, $value, $fail) {
if (! Schema::hasTable($value)) {
$fail(__('badaso::validation.crud.table_not_found', ['table' => $value]));
Expand Down Expand Up @@ -275,7 +277,7 @@ public function add(Request $request)
$request->validate([
'name' => [
'required',
'unique:Uasoft\Badaso\Models\DataType',
new UniqueModel(DataType::class, 'name'),
function ($attribute, $value, $fail) {
if (! Schema::hasTable($value)) {
$fail(__('badaso::validation.crud.table_not_found', ['table' => $value]));
Expand Down Expand Up @@ -405,7 +407,7 @@ public function delete(Request $request)

try {
$request->validate([
'id' => 'required|exists:Uasoft\Badaso\Models\DataType,id',
'id' => ['required', new ExistsModel(DataType::class)],
]);

$data_type = DataType::find($request->id);
Expand Down
3 changes: 2 additions & 1 deletion src/Controllers/BadasoConfigurationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Uasoft\Badaso\Exceptions\SingleException;
use Uasoft\Badaso\Helpers\ApiResponse;
use Uasoft\Badaso\Models\Configuration;
use Uasoft\Badaso\Rules\UniqueModel;
use Uasoft\Badaso\Traits\FileHandler;

class BadasoConfigurationsController extends Controller
Expand Down Expand Up @@ -147,7 +148,7 @@ public function add(Request $request)

try {
$request->validate([
'key' => 'required|unique:Uasoft\Badaso\Models\Configuration',
'key' => ['required', new UniqueModel(Configuration::class, 'key')],
'display_name' => 'required',
'group' => 'required',
'type' => 'required',
Expand Down
4 changes: 3 additions & 1 deletion src/Controllers/BadasoDatabaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Uasoft\Badaso\Database\Schema\SchemaManager;
use Uasoft\Badaso\Facades\Badaso;
use Uasoft\Badaso\Helpers\ApiResponse;
use Uasoft\Badaso\Models\DataType;
use Uasoft\Badaso\Models\Migration;
use Uasoft\Badaso\Rules\UniqueModel;

class BadasoDatabaseController extends Controller
{
Expand All @@ -32,7 +34,7 @@ public function add(Request $request)
$request->validate([
'table' => [
'required',
'unique:Uasoft\Badaso\Models\DataType,slug',
new UniqueModel(DataType::class, 'slug'),
function ($attribute, $value, $fail) {
if (Schema::hasTable($value)) {
$fail(__('badaso::validation.database.table_already_exists', ['table' => $value]));
Expand Down
42 changes: 22 additions & 20 deletions src/Controllers/BadasoMenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Uasoft\Badaso\Models\Menu;
use Uasoft\Badaso\Models\MenuItem;
use Uasoft\Badaso\Models\Permission;
use Uasoft\Badaso\Rules\ExistsModel;
use Uasoft\Badaso\Rules\UniqueModel;

class BadasoMenuController extends Controller
{
Expand Down Expand Up @@ -54,7 +56,7 @@ public function readMenu(Request $request)
{
try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
]);
$menu = Menu::find($request->menu_id);

Expand All @@ -70,8 +72,8 @@ public function readMenuItem(Request $request)
{
try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_item_id' => ['required', 'exists:Uasoft\Badaso\Models\MenuItem,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_item_id' => ['required', new ExistsModel(MenuItem::class)],
]);

$menu_item = MenuItem::where('menu_id', $request->menu_id)->where('id', $request->menu_item_id)->first();
Expand All @@ -88,7 +90,7 @@ public function browseMenuItemByKey(Request $request)
{
try {
$request->validate([
'menu_key' => ['required', 'exists:Uasoft\Badaso\Models\Menu,key'],
'menu_key' => ['required', new ExistsModel(Menu::class, 'key')],
]);
$prefix = config('badaso.database.prefix');
$menu = Menu::where('key', $request->menu_key)->first();
Expand Down Expand Up @@ -182,7 +184,7 @@ public function addMenu(Request $request)

try {
$request->validate([
'key' => ['required', 'unique:Uasoft\Badaso\Models\Menu'],
'key' => ['required', new UniqueModel(Menu::class)],
'display_name' => ['required'],
]);

Expand All @@ -208,7 +210,7 @@ public function addMenuItem(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'title' => ['required'],
'url' => ['required'],
'target' => ['required'],
Expand Down Expand Up @@ -246,8 +248,8 @@ public function editMenu(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'key' => ['required', "unique:Uasoft\Badaso\Models\Menu,id,{$request->menu_id}"],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'key' => ['required', new UniqueModel(Menu::class, 'key', $request->menu_id)],
'display_name' => ['required'],
]);

Expand All @@ -273,8 +275,8 @@ public function editMenuItem(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_item_id' => ['required', 'exists:Uasoft\Badaso\Models\MenuItem,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_item_id' => ['required', new ExistsModel(MenuItem::class)],
'title' => ['required'],
'url' => ['required'],
'target' => ['required'],
Expand Down Expand Up @@ -310,8 +312,8 @@ public function editMenuItemOrder(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_item_id' => ['required', 'exists:Uasoft\Badaso\Models\MenuItem,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_item_id' => ['required', new ExistsModel(MenuItem::class)],
]);
$menu_item = MenuItem::find($request->menu_item_id);
$order = $request->get('order');
Expand Down Expand Up @@ -364,7 +366,7 @@ public function editMenuItemsOrder(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_items' => ['required'],
]);

Expand Down Expand Up @@ -399,7 +401,7 @@ public function deleteMenu(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
]);

Menu::find($request->menu_id)->delete();
Expand All @@ -420,8 +422,8 @@ public function deleteMenuItem(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_item_id' => ['required', 'exists:Uasoft\Badaso\Models\MenuItem,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_item_id' => ['required', new ExistsModel(MenuItem::class)],
]);

MenuItem::find($request->menu_item_id)->delete();
Expand All @@ -440,8 +442,8 @@ public function getMenuItemPermissions(Request $request)
{
try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_item_id' => ['required', 'exists:Uasoft\Badaso\Models\MenuItem,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_item_id' => ['required', new ExistsModel(MenuItem::class)],
]);

$menu_item = MenuItem::find($request->menu_item_id);
Expand Down Expand Up @@ -476,8 +478,8 @@ public function setMenuItemPermissions(Request $request)

try {
$request->validate([
'menu_id' => ['required', 'exists:Uasoft\Badaso\Models\Menu,id'],
'menu_item_id' => ['required', 'exists:Uasoft\Badaso\Models\MenuItem,id'],
'menu_id' => ['required', new ExistsModel(Menu::class)],
'menu_item_id' => ['required', new ExistsModel(MenuItem::class)],
]);

$permission_ids = $request->get('permissions', []);
Expand Down
15 changes: 7 additions & 8 deletions src/Controllers/BadasoPermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Support\Facades\DB;
use Uasoft\Badaso\Helpers\ApiResponse;
use Uasoft\Badaso\Models\Permission;
use Uasoft\Badaso\Rules\ExistsModel;
use Uasoft\Badaso\Rules\UniqueModel;

class BadasoPermissionController extends Controller
{
Expand All @@ -27,7 +29,7 @@ public function read(Request $request)
{
try {
$request->validate([
'id' => 'required|exists:Uasoft\Badaso\Models\Permission,id',
'id' => ['required', new ExistsModel(Permission::class)],
]);

$permission = Permission::find($request->id);
Expand All @@ -46,11 +48,8 @@ public function edit(Request $request)

try {
$request->validate([
'id' => [
'required',
'exists:Uasoft\Badaso\Models\Permission,id',
],
'key' => "required|unique:Uasoft\Badaso\Models\Permission,key,{$request->id}",
'id' => ['required', new ExistsModel(Permission::class, 'id')],
'key' => ['required', new UniqueModel(Permission::class, 'key', $request->id)],
'description' => 'nullable',
'always_allow' => 'required',
'is_public' => 'required',
Expand Down Expand Up @@ -80,7 +79,7 @@ public function add(Request $request)

try {
$request->validate([
'key' => 'required|unique:Uasoft\Badaso\Models\Permission',
'key' => ['required', new UniqueModel(Permission::class, 'key')],
'description' => 'nullable',
'always_allow' => 'required',
'is_public' => 'required',
Expand Down Expand Up @@ -111,7 +110,7 @@ public function delete(Request $request)
$request->validate([
'id' => [
'required',
'exists:Uasoft\Badaso\Models\Permission',
new ExistsModel(Permission::class, 'id'),
],
]);

Expand Down
Loading

0 comments on commit 6019799

Please sign in to comment.