Habilitar user por correo (Solo admi) - validado
This commit is contained in:
parent
827ff6e7c7
commit
80637327ee
@ -5,28 +5,36 @@
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\User;
|
||||
use App\Models\Estandar;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function register(Request $request){
|
||||
$request->validate([
|
||||
'name'=>'required',
|
||||
'lastname'=>'required',
|
||||
'email'=>'required|email|unique:users',
|
||||
'password'=>'required|confirmed'
|
||||
$request->validate([
|
||||
'email'=>'required|email|unique:users'
|
||||
]);
|
||||
$userAuth = auth()->user()->roles[0]->name;
|
||||
if($userAuth==="Admin"){
|
||||
$user = new User();
|
||||
$user->name = "null";
|
||||
$user->lastname = "null";
|
||||
$user->email = $request->email;
|
||||
$user->password = "null";
|
||||
$user->save();
|
||||
$user->roles()->attach(2);
|
||||
return response()->json([
|
||||
'message'=>'Correo registrado exitosamente',
|
||||
'userAuth'=>$user,
|
||||
]);
|
||||
}
|
||||
else{
|
||||
return response()->json([
|
||||
"status" => 0,
|
||||
"message" => "Correo no registrado",
|
||||
], 404);
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->name = $request->name;
|
||||
$user->lastname = $request->lastname;
|
||||
$user->email = $request->email;
|
||||
$user->password = Hash::make($request->password);
|
||||
$user->save();
|
||||
|
||||
return response()->json([
|
||||
'message'=>'registro exitoso'
|
||||
]);
|
||||
}
|
||||
|
||||
public function login(Request $request){
|
||||
|
@ -36,4 +36,10 @@ public function evidencias(){
|
||||
public function providers(){
|
||||
return $this->hasMany(Provider::class,'id_user');
|
||||
}
|
||||
|
||||
public function roles(){
|
||||
return $this->belongsToMany(role::class,'role_user','id_user', 'id_rol');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -8,4 +8,15 @@
|
||||
class role extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table ='roles';
|
||||
|
||||
protected $fillable = [
|
||||
'name'
|
||||
];
|
||||
public $timestamps = false;
|
||||
|
||||
public function users(){
|
||||
return $this->belongsToMany(User::class,'id_rol');
|
||||
}
|
||||
}
|
||||
|
23
database/factories/RoleFactory.php
Normal file
23
database/factories/RoleFactory.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Role>
|
||||
*/
|
||||
class RoleFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->name(),
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('role_user', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->foreignId('id_user')
|
||||
->constrained('users')
|
||||
->onDelete('cascade');
|
||||
$table->foreignId('id_rol')
|
||||
->constrained('roles')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('role_user');
|
||||
}
|
||||
};
|
@ -15,38 +15,63 @@ class DatabaseSeeder extends Seeder
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
//roles
|
||||
\App\Models\role::factory()->create([
|
||||
"name"=>"Admin"
|
||||
]);
|
||||
\App\Models\role::factory()->create([
|
||||
"name"=>"User"
|
||||
]);
|
||||
|
||||
//Usuarios
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Alexis",
|
||||
"lastname"=>"Arroyo",
|
||||
"email"=>"aarroyoh@unsa.edu.pe",
|
||||
"password"=>Hash::make("luisangel"),
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Omar",
|
||||
"lastname"=>"Apoyo",
|
||||
"email"=>"admin@unsa.edu.pe",
|
||||
"password"=>Hash::make("123456"),
|
||||
]);
|
||||
\App\Models\User::find(1)->roles()->attach(1);
|
||||
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Alexis",
|
||||
"lastname"=>"Arroyo",
|
||||
"email"=>"aarroyoh@unsa.edu.pe",
|
||||
"password"=>Hash::make("luisangel"),
|
||||
]);
|
||||
\App\Models\User::find(2)->roles()->attach(1);
|
||||
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Fernando",
|
||||
"lastname"=>"Araoz",
|
||||
"email"=>"faraoz@unsa.edu.pe",
|
||||
"password"=>Hash::make("123456"),
|
||||
]);
|
||||
\App\Models\User::find(3)->roles()->attach(1);
|
||||
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Jhonatan",
|
||||
"lastname"=>"Acuña",
|
||||
"email"=>"jacuna@unsa.edu.pe",
|
||||
"password"=>Hash::make("123456"),
|
||||
]);
|
||||
\App\Models\User::find(4)->roles()->attach(1);
|
||||
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Carlos",
|
||||
"lastname"=>"Gonzales",
|
||||
"email"=>"cgonzalesmo@unsa.edu.pe",
|
||||
"password"=>Hash::make("123456"),
|
||||
]);
|
||||
\App\Models\User::find(5)->roles()->attach(1);
|
||||
|
||||
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Christian",
|
||||
"lastname"=>"Sullca",
|
||||
"email"=>"csullcap@unsa.edu.pe",
|
||||
"password"=>Hash::make("123456"),
|
||||
]);
|
||||
\App\Models\User::find(6)->roles()->attach(1);
|
||||
|
||||
\App\Models\User::factory()->create([
|
||||
"name"=>"Brayan",
|
||||
@ -54,6 +79,7 @@ public function run()
|
||||
"email"=>"bguillenn@unsa.edu.pe",
|
||||
"password"=>Hash::make("123456"),
|
||||
]);
|
||||
\App\Models\User::find(7)->roles()->attach(1);
|
||||
|
||||
//Estandares
|
||||
|
||||
@ -162,5 +188,6 @@ public function run()
|
||||
;\App\Models\EstadosValores::factory()->create([
|
||||
"valor"=>"Concluido",
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@
|
||||
use App\Http\Controllers\Api\LoginController;
|
||||
use App\Http\Controllers\Api\NarrativasController;
|
||||
|
||||
Route::post('register', [UserController::class, 'register']);
|
||||
|
||||
Route::post('login', [UserController::class, 'login'])->name('login');
|
||||
|
||||
Route::get('login/{provider}', [LoginController::class, 'redirectToProvider']);
|
||||
@ -42,6 +42,7 @@
|
||||
//rutas auth
|
||||
Route::get('user-profile', [UserController::class,'userProfile']);
|
||||
Route::get('logout', [UserController::class, 'logout']);
|
||||
Route::post('register', [UserController::class, 'register']);
|
||||
|
||||
//rutas estandar
|
||||
Route::post('estandar', [EstandarController::class,'createEstandar']);
|
||||
|
Loading…
Reference in New Issue
Block a user