This commit is contained in:
Christian Dacid Sullca Puma 2022-10-25 01:38:24 -05:00
commit 55d754e957
7 changed files with 31 additions and 11 deletions

View File

@ -14,17 +14,19 @@ class UserController extends Controller
public function register(Request $request) public function register(Request $request)
{ {
$request->validate([ $request->validate([
'email' => 'required|email|unique:users' 'email' => 'required|email|unique:users',
'rol'=> 'required|numeric|min:1|max:2'
]); ]);
$userAuth = auth()->user()->roles[0]->name; $userAuth = auth()->user()->roles[0]->name;
if ($userAuth === "Admin") { if ($userAuth == "Admin") {
$user = new User(); $user = new User();
$user->name = "null"; $user->name = "null";
$user->lastname = "null"; $user->lastname = "null";
$user->email = $request->email; $user->email = $request->email;
$user->password = "null"; $user->password = "null";
$user->estado = true;
$user->save(); $user->save();
$user->roles()->attach(2); $user->roles()->attach($request->rol);
return response()->json([ return response()->json([
'message' => 'Correo registrado exitosamente', 'message' => 'Correo registrado exitosamente',
'userAuth' => $user, 'userAuth' => $user,
@ -32,7 +34,7 @@ public function register(Request $request)
} else { } else {
return response()->json([ return response()->json([
"status" => 0, "status" => 0,
"message" => "Correo no registrado", "message" => "No eres administrador: Correo no registrado",
], 404); ], 404);
} }
} }
@ -78,6 +80,15 @@ public function userProfile()
]); ]);
} }
public function listUser(){
$users = User::all();
return response([
"status" => 1,
"msg" => "!Lista de usuarios",
"data" => $users,
]);
}
public function logout() public function logout()
{ {
auth()->user()->tokens()->delete(); auth()->user()->tokens()->delete();

View File

@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class role extends Model class Role extends Model
{ {
use HasFactory; use HasFactory;

View File

@ -43,7 +43,7 @@ public function providers()
public function roles() public function roles()
{ {
return $this->belongsToMany(role::class, 'role_user', 'id_user', 'id_rol'); return $this->belongsToMany(Role::class, 'role_user', 'id_user', 'id_rol');
} }
public function isAdmin() public function isAdmin()

View File

@ -19,6 +19,7 @@ public function up()
$table->string('lastname'); $table->string('lastname');
$table->string('email')->unique(); $table->string('email')->unique();
$table->string('password'); $table->string('password');
$table->boolean('estado');
}); });
} }

View File

@ -16,10 +16,10 @@ class DatabaseSeeder extends Seeder
public function run() public function run()
{ {
//roles //roles
\App\Models\role::factory()->create([ \App\Models\Role::factory()->create([
"name"=>"Admin" "name"=>"Admin"
]); ]);
\App\Models\role::factory()->create([ \App\Models\Role::factory()->create([
"name"=>"User" "name"=>"User"
]); ]);
@ -29,6 +29,7 @@ public function run()
"lastname"=>"Apoyo", "lastname"=>"Apoyo",
"email"=>"admin@unsa.edu.pe", "email"=>"admin@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
"estado"=>true,
]); ]);
\App\Models\User::find(1)->roles()->attach(1); \App\Models\User::find(1)->roles()->attach(1);
@ -37,6 +38,7 @@ public function run()
"lastname"=>"Arroyo", "lastname"=>"Arroyo",
"email"=>"aarroyoh@unsa.edu.pe", "email"=>"aarroyoh@unsa.edu.pe",
"password"=>Hash::make("luisangel"), "password"=>Hash::make("luisangel"),
"estado"=>true,
]); ]);
\App\Models\User::find(2)->roles()->attach(1); \App\Models\User::find(2)->roles()->attach(1);
@ -45,6 +47,7 @@ public function run()
"lastname"=>"Araoz", "lastname"=>"Araoz",
"email"=>"faraoz@unsa.edu.pe", "email"=>"faraoz@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
"estado"=>true,
]); ]);
\App\Models\User::find(3)->roles()->attach(1); \App\Models\User::find(3)->roles()->attach(1);
@ -53,6 +56,7 @@ public function run()
"lastname"=>"Acuña", "lastname"=>"Acuña",
"email"=>"jacuna@unsa.edu.pe", "email"=>"jacuna@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
"estado"=>true,
]); ]);
\App\Models\User::find(4)->roles()->attach(1); \App\Models\User::find(4)->roles()->attach(1);
@ -61,6 +65,7 @@ public function run()
"lastname"=>"Gonzales", "lastname"=>"Gonzales",
"email"=>"cgonzalesmo@unsa.edu.pe", "email"=>"cgonzalesmo@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
"estado"=>true,
]); ]);
\App\Models\User::find(5)->roles()->attach(1); \App\Models\User::find(5)->roles()->attach(1);
@ -70,6 +75,7 @@ public function run()
"lastname"=>"Sullca", "lastname"=>"Sullca",
"email"=>"csullcap@unsa.edu.pe", "email"=>"csullcap@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
"estado"=>true,
]); ]);
\App\Models\User::find(6)->roles()->attach(1); \App\Models\User::find(6)->roles()->attach(1);
@ -78,6 +84,7 @@ public function run()
"lastname"=>"Guillen", "lastname"=>"Guillen",
"email"=>"bguillenn@unsa.edu.pe", "email"=>"bguillenn@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
"estado"=>true,
]); ]);
\App\Models\User::find(7)->roles()->attach(1); \App\Models\User::find(7)->roles()->attach(1);

View File

@ -41,6 +41,7 @@
Route::middleware("auth:sanctum")->group(function () { Route::middleware("auth:sanctum")->group(function () {
//rutas auth //rutas auth
Route::get('user-profile', [UserController::class, 'userProfile']); Route::get('user-profile', [UserController::class, 'userProfile']);
Route::get('user', [UserController::class, 'listUser']);
Route::get('logout', [UserController::class, 'logout']); Route::get('logout', [UserController::class, 'logout']);
Route::post('register', [UserController::class, 'register']); Route::post('register', [UserController::class, 'register']);
@ -116,6 +117,6 @@
Route::get('narrativa', [NarrativasController::class, 'listNarrativas']); Route::get('narrativa', [NarrativasController::class, 'listNarrativas']);
}); });
Route::middleware('auth:sanctum')->get('/user', function (Request $request) { /*Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user(); return $request->user();
}); });*/