Commit 9f3d6ece authored by Alfonso Rafael Solis Rangel's avatar Alfonso Rafael Solis Rangel
Browse files

Merge branch 'usuarios' into 'main'

Usuarios

See merge request !13
parents 06c235cf 76c03a5a
Loading
Loading
Loading
Loading
+108 −0
Original line number Diff line number Diff line
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateRequestUser;
use App\Models\Dependencia;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $usuarios = User::with('dependencia', 'roles')->get();
        return view('adminGen.usuarios.index', ['usuarios' => $usuarios]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $dependencias = Dependencia::get();
        $roles = Role::get();
        return view('adminGen.usuarios.create', compact('dependencias', 'roles'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreUserRequest $request)
    {
        $request->validated();
        $user = new User;
        $user->name = $request->name;
        $user->username = $request->username;
        $user->password = Hash::make($request->password);
        $user->dependencia_id = $request->dependencia_id;
        $user->assignRole($request->role);
        if($request->active == "on"){
            $user->active = 1;
        }else{
            $user->active = 0;
        }
        $user->save();
        return redirect()->route('usuarios.get')->with('success', 'Usuario creado correctamente.');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $user = User::findOrFail($id);
        $dependencias = Dependencia::all();
        $roles = Role::all();
        return view('adminGen.usuarios.edit', compact('user', 'dependencias', 'roles'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRequestUser $request, string $id)
    {
        $request->validated();
        $user = User::findOrFail($id);

        $user->name = $request->name;
        $user->username = $request->username;
        if ($request->filled('password')) {
            $user->password = Hash::make($request->password);
        }
        $user->dependencia_id = $request->dependencia_id;
        $user->assignRole($request->role);
        $user->active = $request->has('active');
        $user->save();

        return redirect()->route('usuarios.get')->with('success', 'Usuario actualizado correctamente.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        try{
            $usuario = User::findOrFail($id);
            $usuario->delete();
            return redirect()->route('usuarios.get')->with('success', 'Usuario eliminado correctamente.');
        }catch(\Exception $e){
            return redirect()->route('usuarios.get')->withErrors('Error al eliminar el usuario:' . $e->getMessage());
        }
    }
}
+32 −0
Original line number Diff line number Diff line
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required|regex:/\w*$/|string|min:1|max:255',
            'username' => 'required|string|regex:/\w*$/|max:255|unique:users,username',
            'password' => 'required|regex:/\w*$/|string|min:8|max:255|confirmed',
            'dependencia_id' => 'required|min:1|integer',
            'role' => 'required|string|min:1'
        ];
    }
}
+34 −0
Original line number Diff line number Diff line
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateRequestUser extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $id = $this->route('id');
        return [
            'name' => 'required|string|max:255',
            'username' => ['required', 'string', 'max:255', Rule::unique('users')->ignore($id)],
            'password' => 'nullable|string|min:8|confirmed',
            'dependencia_id' => 'required|exists:dependencias,id',
            'role' => 'required|exists:roles,name',
        ];
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
@@ -31,6 +32,7 @@ class User extends Authenticatable
        'username',
        'active',
        'password',
        'dependencia_id'
    ];

    /**
@@ -66,4 +68,9 @@ protected function casts(): array
            'password' => 'hashed',
        ];
    }

    public function dependencia(): BelongsTo
    {
        return $this->belongsTo(Dependencia::class);
    }
}
+133 −0
Original line number Diff line number Diff line
/**
 * jQuery toast plugin created by Kamran Ahmed copyright MIT license 2014
 */
.jq-toast-wrap { 
    display: block; 
    position: fixed; 
    width: 250px;  
    pointer-events: none !important; 
    margin: 0; 
    padding: 0; 
    letter-spacing: normal; 
    z-index: 9000 !important; 
}
.jq-toast-wrap * { 
    margin: 0; 
    padding: 0; 
}

.jq-toast-wrap.bottom-left { 
    bottom: 20px; 
    left: 20px; 
}
.jq-toast-wrap.bottom-right { 
    bottom: 20px; 
    right: 40px; 
}
.jq-toast-wrap.top-left { 
    top: 20px; 
    left: 20px; 
}
.jq-toast-wrap.top-right { 
    top: 20px; 
    right: 80px; 
}

.jq-toast-single { 
    display: block; 
    width: 100%; 
    padding: 10px; 
    margin: 0px 0px 5px; 
    border-radius: 4px; 
    font-size: 13px; 
    font-family: arial, sans-serif; 
    line-height: 17px; 
    position: relative;  
    pointer-events: all !important; 
    background-color: #444444; 
    color: white; 
}

.jq-toast-single h2 { 
    font-family: arial, sans-serif; 
    font-size: 14px; 
    margin: 0px 0px 7px; 
    background: none; 
    
    color: inherit; 
    line-height: inherit; 
    letter-spacing: normal; 
}
.jq-toast-single a { 
    color: #eee; 
    text-decoration: none; 
    font-weight: bold; 
    border-bottom: 1px solid white; 
    padding-bottom: 3px; 
    font-size: 12px; 
}

.jq-toast-single ul { 
    margin: 0px 0px 0px 15px; 
    background: none; 
    padding:0px; 
}
.jq-toast-single ul li { 
    list-style-type: disc !important; 
    line-height: 17px; 
    background: none; 
    margin: 0; 
    padding: 0; 
    letter-spacing: normal; 
}

.close-jq-toast-single { 
    position: absolute; 
    top: 3px; 
    right: 7px; 
    font-size: 14px; 
    cursor: pointer; 
}

.jq-toast-loader { 
    display: block; 
    position: absolute; 
    top: -2px; 
    height: 5px; 
    width: 0%; 
    left: 0; 
    border-radius: 5px; 
    background: #D1EDD6; 
}
.jq-toast-loaded { 
    width: 100%; 
}
.jq-has-icon { 
    padding: 10px 10px 10px 50px;
    background-repeat: no-repeat; 
    background-position: 10px; 
}
.jq-icon-info { 
    background-image: url(/assets/images/toast/infocircle.png); 
    background-color: #F1F7FA;
    color: #d9edf7;
    border-color: #bce8f1;
}
.jq-icon-warning { 
    background-image: url(/assets/images/toast/warning2.png); 
    background-color: #FAF0E4;
    color: #fcf8e3;
    border-color: #faebcc;
}
.jq-icon-error { 
    background-image: url(/assets/images/toast/error_circle.png);
    background-color: #F8E2E1;
    color: #676767;
    border-color: #676767;
}
.jq-icon-success { 
    background-image: url(/assets/images/toast/icon_check_circle.png); 
    background-color: #F1FAF3;
    color: #676767;
    border-color: #F1FAF3;
}
 No newline at end of file
Loading