Skip to content

Commit 19ae92d

Browse files
committed
v1
1 parent 450b7af commit 19ae92d

16 files changed

+217
-42
lines changed

Diff for: app/Http/Controllers/Auth/RegisterController.php

+10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ protected function validator(array $data)
6464
*/
6565
protected function create(array $data)
6666
{
67+
if(User::count()==0){
68+
//first registration, admin privileges
69+
$active=1;
70+
$role=0;
71+
} else {
72+
$active=0;
73+
$role=10;
74+
}
6775
return User::create([
76+
'active' => $active,
77+
'role' => $role,
6878
'name' => $data['name'],
6979
'email' => $data['email'],
7080
'password' => Hash::make($data['password']),

Diff for: app/Http/Controllers/CategoryController.php

+32-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111
*/
1212
class CategoryController extends Controller
1313
{
14+
public function __construct()
15+
{
16+
$this->middleware('auth');
17+
}
1418
/**
1519
* Display a listing of the resource.
1620
*
1721
* @return \Illuminate\Http\Response
1822
*/
1923
public function index()
2024
{
25+
if (auth()->user()->role > 0){
26+
return redirect()->route('home')
27+
->with('error', 'No tiene permisos para realizar esta operación.');
28+
}
2129
$categories = Category::paginate();
22-
2330
return view('category.index', compact('categories'))
2431
->with('i', (request()->input('page', 1) - 1) * $categories->perPage());
2532
}
@@ -31,6 +38,10 @@ public function index()
3138
*/
3239
public function create()
3340
{
41+
if (auth()->user()->role > 0){
42+
return redirect()->route('home')
43+
->with('error', 'No tiene permisos para realizar esta operación.');
44+
}
3445
$category = new Category();
3546
return view('category.create', compact('category'));
3647
}
@@ -43,10 +54,12 @@ public function create()
4354
*/
4455
public function store(Request $request)
4556
{
57+
if (auth()->user()->role > 0){
58+
return redirect()->route('home')
59+
->with('error', 'No tiene permisos para realizar esta operación.');
60+
}
4661
request()->validate(Category::$rules);
47-
4862
$category = Category::create($request->all());
49-
5063
return redirect()->route('categories.index')
5164
->with('success', 'Categoría creada con éxito.');
5265
}
@@ -59,8 +72,11 @@ public function store(Request $request)
5972
*/
6073
public function show($id)
6174
{
75+
if (auth()->user()->role > 1){
76+
return redirect()->route('home')
77+
->with('error', 'No tiene permisos para realizar esta operación.');
78+
}
6279
$category = Category::find($id);
63-
6480
return view('category.show', compact('category'));
6581
}
6682

@@ -72,8 +88,11 @@ public function show($id)
7288
*/
7389
public function edit($id)
7490
{
91+
if (auth()->user()->role > 0){
92+
return redirect()->route('home')
93+
->with('error', 'No tiene permisos para realizar esta operación.');
94+
}
7595
$category = Category::find($id);
76-
7796
return view('category.edit', compact('category'));
7897
}
7998

@@ -86,8 +105,11 @@ public function edit($id)
86105
*/
87106
public function update(Request $request, Category $category)
88107
{
108+
if (auth()->user()->role > 0){
109+
return redirect()->route('home')
110+
->with('error', 'No tiene permisos para realizar esta operación.');
111+
}
89112
request()->validate(Category::$rules);
90-
91113
$category->update($request->all());
92114

93115
return redirect()->route('categories.index')
@@ -101,8 +123,11 @@ public function update(Request $request, Category $category)
101123
*/
102124
public function destroy($id)
103125
{
126+
if (auth()->user()->role > 0){
127+
return redirect()->route('home')
128+
->with('error', 'No tiene permisos para realizar esta operación.');
129+
}
104130
$category = Category::find($id)->delete();
105-
106131
return redirect()->route('categories.index')
107132
->with('success', 'Categoría elimnada con éxito.');
108133
}

Diff for: app/Http/Controllers/OrderController.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
class OrderController extends Controller
1818
{
19+
public function __construct()
20+
{
21+
$this->middleware('auth');
22+
}
1923
/**
2024
* Display a listing of the resource.
2125
*
@@ -36,6 +40,10 @@ public function index()
3640
*/
3741
public function create()
3842
{
43+
if (auth()->user()->role > 1){
44+
return redirect()->route('home')
45+
->with('error', 'No tiene permisos para realizar esta operación.');
46+
}
3947
$order = new Order();
4048
$category = Category::all();
4149
$product = Product::all();
@@ -64,12 +72,25 @@ public function status($id_order,$id_status)
6472
*/
6573
public function store(Request $request)
6674
{
67-
$id_order = rand(100, 999);
75+
if (auth()->user()->role > 1){
76+
return redirect()->route('home')
77+
->with('error', 'No tiene permisos para realizar esta operación.');
78+
}
79+
$data = Order::latest('id')->first();
80+
if($data != null){
81+
$id_order = $data->id + 1;
82+
}else{$id_order = 1;}
83+
$id = DB::table('clients')->insertGetId(
84+
[ 'id_order' => $id_order,
85+
'name' => $request['name'],
86+
'notes' => $request['notes']
87+
]
88+
);
6889
foreach ($request->p as $x => $x_value){
6990
if($x_value>0){
7091
$order = new Order;
7192
$order->id_order = $id_order;
72-
$order->id_user = auth()->user()->id;
93+
$order->id_client = $id;
7394
$order->id_product = (int)$x;
7495
$order->quantity = (int)$x_value;
7596
$order->status = 0;
@@ -118,9 +139,7 @@ public function edit($id)
118139
public function update(Request $request, Order $order)
119140
{
120141
request()->validate(Order::$rules);
121-
122142
$order->update($request->all());
123-
124143
return redirect()->route('orders.index')
125144
->with('success', 'Pedido modificado con éxito.');
126145
}
@@ -133,7 +152,6 @@ public function update(Request $request, Order $order)
133152
public function destroy($id)
134153
{
135154
$order = Order::find($id)->delete();
136-
137155
return redirect()->route('orders.index')
138156
->with('success', 'Pedido eliminado con éxito');
139157
}

Diff for: app/Http/Controllers/ProductController.php

+32-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212
*/
1313
class ProductController extends Controller
1414
{
15+
public function __construct()
16+
{
17+
$this->middleware('auth');
18+
}
1519
/**
1620
* Display a listing of the resource.
1721
*
1822
* @return \Illuminate\Http\Response
1923
*/
2024
public function index()
2125
{
26+
if (auth()->user()->role > 1){
27+
return redirect()->route('home')
28+
->with('error', 'No tiene permisos para realizar esta operación.');
29+
}
2230
$products = Product::paginate();
23-
2431
return view('product.index', compact('products'))
2532
->with('i', (request()->input('page', 1) - 1) * $products->perPage());
2633
}
@@ -32,6 +39,10 @@ public function index()
3239
*/
3340
public function create()
3441
{
42+
if (auth()->user()->role > 0){
43+
return redirect()->route('home')
44+
->with('error', 'No tiene permisos para realizar esta operación.');
45+
}
3546
$product = new Product();
3647
$category = Category::all();
3748

@@ -46,10 +57,12 @@ public function create()
4657
*/
4758
public function store(Request $request)
4859
{
60+
if (auth()->user()->role > 0){
61+
return redirect()->route('home')
62+
->with('error', 'No tiene permisos para realizar esta operación.');
63+
}
4964
request()->validate(Product::$rules);
50-
5165
$product = Product::create($request->all());
52-
5366
return redirect()->route('products.index')
5467
->with('success', 'Producto añadido con éxito.');
5568
}
@@ -62,6 +75,10 @@ public function store(Request $request)
6275
*/
6376
public function show($id)
6477
{
78+
if (auth()->user()->role > 1){
79+
return redirect()->route('home')
80+
->with('error', 'No tiene permisos para realizar esta operación.');
81+
}
6582
$product = Product::find($id);
6683

6784
return view('product.show', compact('product'));
@@ -75,6 +92,10 @@ public function show($id)
7592
*/
7693
public function edit($id)
7794
{
95+
if (auth()->user()->role > 1){
96+
return redirect()->route('home')
97+
->with('error', 'No tiene permisos para realizar esta operación.');
98+
}
7899
$product = Product::find($id);
79100
$category = Category::all();
80101

@@ -90,10 +111,12 @@ public function edit($id)
90111
*/
91112
public function update(Request $request, Product $product)
92113
{
114+
if (auth()->user()->role > 1){
115+
return redirect()->route('home')
116+
->with('error', 'No tiene permisos para realizar esta operación.');
117+
}
93118
request()->validate(Product::$rules);
94-
95119
$product->update($request->all());
96-
97120
return redirect()->route('products.index')
98121
->with('success', 'Producto actualizado con éxito.');
99122
}
@@ -105,8 +128,11 @@ public function update(Request $request, Product $product)
105128
*/
106129
public function destroy($id)
107130
{
131+
if (auth()->user()->role > 0){
132+
return redirect()->route('home')
133+
->with('error', 'No tiene permisos para realizar esta operación.');
134+
}
108135
$product = Product::find($id)->delete();
109-
110136
return redirect()->route('products.index')
111137
->with('success', 'Producto eliminado con éxito.');
112138
}

Diff for: app/Http/Controllers/UserController.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@
55
use App\Models\User;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\Hash;
8+
use Illuminate\Support\Facades\Auth;
9+
10+
11+
//use Auth;
812

913
/**
1014
* Class UserController
1115
* @package App\Http\Controllers
1216
*/
1317
class UserController extends Controller
1418
{
19+
public function __construct()
20+
{
21+
$this->middleware('auth');
22+
23+
}
1524
/**
1625
* Display a listing of the resource.
1726
*
1827
* @return \Illuminate\Http\Response
1928
*/
2029
public function index()
2130
{
31+
if (auth()->user()->role > 1){
32+
return redirect()->route('home')
33+
->with('error', 'No tiene permisos para realizar esta operación.');
34+
}
2235
$users = User::paginate();
23-
2436
return view('user.index', compact('users'))
2537
->with('i', (request()->input('page', 1) - 1) * $users->perPage());
2638
}

Diff for: app/Models/Client.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Client extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = ['name','notes'];
13+
14+
}

Diff for: app/Models/Order.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Order extends Model
2626

2727
static $rules = [
2828
'id_order' => 'required',
29-
'id_user' => 'required',
29+
'id_client' => 'required',
3030
'id_product' => 'required',
3131
'quantity' => 'required',
3232
'status' => 'required',
@@ -39,7 +39,7 @@ class Order extends Model
3939
*
4040
* @var array
4141
*/
42-
protected $fillable = ['id_order','id_user','id_product','quantity','status'];
42+
protected $fillable = ['id_order','id_client','id_product','quantity','status'];
4343

4444

4545
/**
@@ -49,14 +49,16 @@ public function product()
4949
{
5050
return $this->hasOne('App\Models\Product', 'id', 'id_product');
5151
}
52-
52+
5353
/**
5454
* @return \Illuminate\Database\Eloquent\Relations\HasOne
5555
*/
56-
public function user()
56+
public function client()
5757
{
58-
return $this->hasOne('App\Models\User', 'id', 'id_user');
58+
return $this->hasOne('App\Models\Client', 'id', 'id_client');
5959
}
60+
61+
6062

6163

6264
}

Diff for: database/migrations/2014_10_12_000000_create_users_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function up()
1818
$table->string('name');
1919
$table->string('email')->unique();
2020
$table->boolean('active')->default(0)->nullable();
21-
$table->integer('role')->default(-1);
21+
$table->integer('role')->default(10);
2222
$table->timestamp('email_verified_at')->nullable();
2323
$table->string('password');
2424
$table->rememberToken();

Diff for: database/migrations/2021_10_27_000115_create_orders_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function up()
1616
Schema::create('orders', function (Blueprint $table) {
1717
$table->id();
1818
$table->unsignedBigInteger('id_order');
19-
$table->unsignedBigInteger('id_user');
20-
$table->foreign('id_user')->references('id')->on('users')->onDelete('cascade');
19+
$table->unsignedBigInteger('id_client');
20+
$table->foreign('id_client')->references('id')->on('clients')->onDelete('cascade');
2121
$table->unsignedBigInteger('id_product');
2222
$table->foreign('id_product')->references('id')->on('products')->onDelete('cascade');
2323
$table->unsignedBigInteger('quantity');

0 commit comments

Comments
 (0)