Habilitar user por correo (Solo admi) - validado

This commit is contained in:
cgonzalesmo 2022-10-19 22:15:33 -05:00
parent 827ff6e7c7
commit 80637327ee
7 changed files with 134 additions and 21 deletions

View File

@ -5,29 +5,37 @@
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\User; use App\Models\User;
use App\Models\Estandar;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
class UserController extends Controller class UserController extends Controller
{ {
public function register(Request $request){ public function register(Request $request){
$request->validate([ $request->validate([
'name'=>'required', 'email'=>'required|email|unique:users'
'lastname'=>'required',
'email'=>'required|email|unique:users',
'password'=>'required|confirmed'
]); ]);
$userAuth = auth()->user()->roles[0]->name;
if($userAuth==="Admin"){
$user = new User(); $user = new User();
$user->name = $request->name; $user->name = "null";
$user->lastname = $request->lastname; $user->lastname = "null";
$user->email = $request->email; $user->email = $request->email;
$user->password = Hash::make($request->password); $user->password = "null";
$user->save(); $user->save();
$user->roles()->attach(2);
return response()->json([ return response()->json([
'message'=>'registro exitoso' 'message'=>'Correo registrado exitosamente',
'userAuth'=>$user,
]); ]);
} }
else{
return response()->json([
"status" => 0,
"message" => "Correo no registrado",
], 404);
}
}
public function login(Request $request){ public function login(Request $request){

View File

@ -36,4 +36,10 @@ public function evidencias(){
public function providers(){ public function providers(){
return $this->hasMany(Provider::class,'id_user'); return $this->hasMany(Provider::class,'id_user');
} }
public function roles(){
return $this->belongsToMany(role::class,'role_user','id_user', 'id_rol');
}
} }

View File

@ -8,4 +8,15 @@
class role extends Model class role extends Model
{ {
use HasFactory; use HasFactory;
protected $table ='roles';
protected $fillable = [
'name'
];
public $timestamps = false;
public function users(){
return $this->belongsToMany(User::class,'id_rol');
}
} }

View 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(),
];
}
}

View File

@ -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');
}
};

View File

@ -15,38 +15,63 @@ class DatabaseSeeder extends Seeder
*/ */
public function run() public function run()
{ {
//roles
\App\Models\role::factory()->create([
"name"=>"Admin"
]);
\App\Models\role::factory()->create([
"name"=>"User"
]);
//Usuarios //Usuarios
\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([ \App\Models\User::factory()->create([
"name"=>"Alexis", "name"=>"Alexis",
"lastname"=>"Arroyo", "lastname"=>"Arroyo",
"email"=>"aarroyoh@unsa.edu.pe", "email"=>"aarroyoh@unsa.edu.pe",
"password"=>Hash::make("luisangel"), "password"=>Hash::make("luisangel"),
]); ]);
\App\Models\User::find(2)->roles()->attach(1);
\App\Models\User::factory()->create([ \App\Models\User::factory()->create([
"name"=>"Fernando", "name"=>"Fernando",
"lastname"=>"Araoz", "lastname"=>"Araoz",
"email"=>"faraoz@unsa.edu.pe", "email"=>"faraoz@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
]); ]);
\App\Models\User::find(3)->roles()->attach(1);
\App\Models\User::factory()->create([ \App\Models\User::factory()->create([
"name"=>"Jhonatan", "name"=>"Jhonatan",
"lastname"=>"Acuña", "lastname"=>"Acuña",
"email"=>"jacuna@unsa.edu.pe", "email"=>"jacuna@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
]); ]);
\App\Models\User::find(4)->roles()->attach(1);
\App\Models\User::factory()->create([ \App\Models\User::factory()->create([
"name"=>"Carlos", "name"=>"Carlos",
"lastname"=>"Gonzales", "lastname"=>"Gonzales",
"email"=>"cgonzalesmo@unsa.edu.pe", "email"=>"cgonzalesmo@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
]); ]);
\App\Models\User::find(5)->roles()->attach(1);
\App\Models\User::factory()->create([ \App\Models\User::factory()->create([
"name"=>"Christian", "name"=>"Christian",
"lastname"=>"Sullca", "lastname"=>"Sullca",
"email"=>"csullcap@unsa.edu.pe", "email"=>"csullcap@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
]); ]);
\App\Models\User::find(6)->roles()->attach(1);
\App\Models\User::factory()->create([ \App\Models\User::factory()->create([
"name"=>"Brayan", "name"=>"Brayan",
@ -54,6 +79,7 @@ public function run()
"email"=>"bguillenn@unsa.edu.pe", "email"=>"bguillenn@unsa.edu.pe",
"password"=>Hash::make("123456"), "password"=>Hash::make("123456"),
]); ]);
\App\Models\User::find(7)->roles()->attach(1);
//Estandares //Estandares
@ -162,5 +188,6 @@ public function run()
;\App\Models\EstadosValores::factory()->create([ ;\App\Models\EstadosValores::factory()->create([
"valor"=>"Concluido", "valor"=>"Concluido",
]); ]);
} }
} }

View File

@ -20,7 +20,7 @@
use App\Http\Controllers\Api\LoginController; use App\Http\Controllers\Api\LoginController;
use App\Http\Controllers\Api\NarrativasController; use App\Http\Controllers\Api\NarrativasController;
Route::post('register', [UserController::class, 'register']);
Route::post('login', [UserController::class, 'login'])->name('login'); Route::post('login', [UserController::class, 'login'])->name('login');
Route::get('login/{provider}', [LoginController::class, 'redirectToProvider']); Route::get('login/{provider}', [LoginController::class, 'redirectToProvider']);
@ -42,6 +42,7 @@
//rutas auth //rutas auth
Route::get('user-profile', [UserController::class,'userProfile']); Route::get('user-profile', [UserController::class,'userProfile']);
Route::get('logout', [UserController::class, 'logout']); Route::get('logout', [UserController::class, 'logout']);
Route::post('register', [UserController::class, 'register']);
//rutas estandar //rutas estandar
Route::post('estandar', [EstandarController::class,'createEstandar']); Route::post('estandar', [EstandarController::class,'createEstandar']);