mirror of
https://github.com/robonen/education-project.git
synced 2026-03-20 02:44:31 +00:00
auth
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
@@ -23,6 +24,11 @@ class Handler extends ExceptionHandler
|
||||
return parent::render($request, $exception);
|
||||
}
|
||||
|
||||
protected function unauthenticated($request, AuthenticationException $exception)
|
||||
{
|
||||
return response()->json(['message' => 'Unauthenticated'], 401);
|
||||
}
|
||||
|
||||
/**
|
||||
* A list of the exception types that are not reported.
|
||||
*
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Auth\LoginRequest;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
@@ -11,14 +12,13 @@ class LoginController extends Controller
|
||||
{
|
||||
$credentials = $request->only('login', 'password');
|
||||
|
||||
if (auth()->attempt($credentials))
|
||||
if (!auth()->attempt($credentials))
|
||||
return response()->json('You cannot sign with those credentials!', 401);
|
||||
|
||||
$token = auth()->user()->makeToken($request->get('remember_me'));
|
||||
$token = auth()->user()->createToken(config('app.name'));
|
||||
$token->token->expires_at = (bool)$request->get('remember_me') ? Carbon::now()->addMonth() : Carbon::now()->addDay();
|
||||
$token->token->save();
|
||||
|
||||
return response()->json([
|
||||
'token_type' => 'Bearer',
|
||||
'token' => $token->accessToken,
|
||||
], 200);
|
||||
return response()->json(['access_token' => $token->accessToken], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ class LogoutController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$request->user()->token()->revoke();
|
||||
auth()->user()->token()->revoke();
|
||||
|
||||
return response()->json('ok', 200);
|
||||
return response()->json(['message' => 'You are logged out'], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,10 @@ class RegisterController extends Controller
|
||||
{
|
||||
public function __invoke(RegisterRequest $request)
|
||||
{
|
||||
$role = Role::where('name', $request->get('role'));
|
||||
$role = Role::where('name', $request->get('role'))->get();
|
||||
|
||||
if ($role->isEmpty())
|
||||
return response()->json('Role not found', 404);
|
||||
return response()->json(['message'=>'Role not found'], 404);
|
||||
|
||||
$user = User::create(array_merge(
|
||||
$request->only('login', 'class_id'),
|
||||
@@ -29,29 +29,25 @@ class RegisterController extends Controller
|
||||
]
|
||||
));
|
||||
|
||||
$user_id = [
|
||||
'user_id' => $user->id,
|
||||
];
|
||||
|
||||
switch($request->get('role'))
|
||||
{
|
||||
case 'headteacher':
|
||||
HeadTeacher::create($user_id);
|
||||
$user->headteacher()->create();
|
||||
break;
|
||||
|
||||
case 'teacher':
|
||||
Teacher::create($user_id);
|
||||
$user->teacher()->create();
|
||||
break;
|
||||
|
||||
case 'student':
|
||||
Student::create($user_id);
|
||||
$user->student()->create();
|
||||
break;
|
||||
|
||||
case 'parent':
|
||||
Parentt::create($user_id);
|
||||
$user->parent()->create();
|
||||
break;
|
||||
}
|
||||
|
||||
return response()->json('ok', 200);
|
||||
return response()->json(null, 201);
|
||||
}
|
||||
}
|
||||
|
||||
65
app/Http/Controllers/ChatLinkController.php
Normal file
65
app/Http/Controllers/ChatLinkController.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ChatLinkRequest;
|
||||
use App\Models\ChatLink;
|
||||
use Illuminate\Database\QueryException;
|
||||
|
||||
class ChatLinkController extends Controller
|
||||
{
|
||||
//Получение ссылок для класса или для их создателя
|
||||
public function index()
|
||||
{
|
||||
switch(auth()->user()->role->name)
|
||||
{
|
||||
case 'student':
|
||||
$links = ChatLink::all()->where('class_id', auth()->user()->class_id);
|
||||
break;
|
||||
|
||||
case 'teacher':
|
||||
case 'headteacher':
|
||||
$links = auth()->user()->chatLinks;
|
||||
break;
|
||||
|
||||
case 'parent':
|
||||
$links = [];
|
||||
break;
|
||||
}
|
||||
|
||||
return response()->json($links, 200);
|
||||
}
|
||||
|
||||
//Создание ссылки
|
||||
public function store(ChatLinkRequest $request)
|
||||
{
|
||||
try {
|
||||
$link = auth()->user()->create($request->all());
|
||||
} catch (QueryException $e) {
|
||||
return response()->json(['message'=>'Class not found'], 404);
|
||||
}
|
||||
|
||||
return response()->json($link, 201);
|
||||
}
|
||||
|
||||
//Обновление ссылки
|
||||
public function update(ChatLink $link, ChatLinkRequest $request)
|
||||
{
|
||||
try {
|
||||
$link->update($request->all());
|
||||
} catch (QueryException $e) {
|
||||
return response()->json(['message'=>'Class not found'], 404);
|
||||
}
|
||||
|
||||
return response()->json($link, 200);
|
||||
}
|
||||
|
||||
//Удаление ссылки
|
||||
public function destroy(ChatLink $link)
|
||||
{
|
||||
$link->delete();
|
||||
return response()->json(null, 204);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,9 +45,8 @@ class TimetableController extends Controller
|
||||
'class' => $class,
|
||||
]);
|
||||
}
|
||||
|
||||
$dateTimetables = [];
|
||||
if (!$filterTimetables->isEmpty()) {
|
||||
//if (!$filterTimetables->isEmpty()) {
|
||||
for ($i = 0; $i < 6; $i++) {
|
||||
$date = Carbon::parse($request->input('date'))
|
||||
->startOfWeek()
|
||||
@@ -55,7 +54,7 @@ class TimetableController extends Controller
|
||||
->format('Y-m-d');
|
||||
array_push($dateTimetables, [$date => $filterTimetables->where('date', $date)->values()]);
|
||||
}
|
||||
}
|
||||
//}
|
||||
return response()->json($dateTimetables, 200);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ class Authenticate extends Middleware
|
||||
*/
|
||||
protected function redirectTo($request)
|
||||
{
|
||||
if (! $request->expectsJson()) {
|
||||
return route('login');
|
||||
}
|
||||
//if (! $request->expectsJson()) {
|
||||
// return route('login');
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class Role
|
||||
$roles = explode($this->delimetr, $roles);
|
||||
|
||||
if (!auth()->user()->hasRole($roles)) {
|
||||
return response()->json('',404);
|
||||
return response()->json(null,404);
|
||||
}
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@ class RegisterRequest extends UserRequest
|
||||
{
|
||||
return [
|
||||
'role' => 'required|string',
|
||||
'class_id' => 'required|integer|gt:0',
|
||||
'class_id' => 'integer|gt:0',
|
||||
'login' => 'required|unique:users'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ abstract class UserRequest extends ApiFormRequest
|
||||
public function rules()
|
||||
{
|
||||
return array_merge([
|
||||
'login' => 'required|string',
|
||||
'password' => 'required|string|min:6',
|
||||
], $this->specific());
|
||||
}
|
||||
|
||||
22
app/Http/Requests/ChatLinkRequest.php
Normal file
22
app/Http/Requests/ChatLinkRequest.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChatLinkRequest extends ApiFormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'link' => 'required',
|
||||
'class_id' => 'required|integer|gt:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
29
app/Models/ChatLink.php
Normal file
29
app/Models/ChatLink.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ChatLink extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'link',
|
||||
'class_id',
|
||||
];
|
||||
|
||||
public function schoolClass()
|
||||
{
|
||||
return $this->belongsTo(SchoolClass::class, 'class_id');
|
||||
}
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->hasMany(User::class, 'creator');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ class HeadTeacher extends Model
|
||||
|
||||
protected $guarded = [
|
||||
'user_id',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
public function user()
|
||||
|
||||
@@ -12,6 +12,7 @@ class Parentt extends Model
|
||||
|
||||
protected $guarded = [
|
||||
'user_id',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
public function user()
|
||||
|
||||
@@ -26,4 +26,10 @@ class SchoolClass extends Model
|
||||
->withPivot('hours_per_week', 'hours_per_year');
|
||||
}
|
||||
|
||||
public function chatLinks()
|
||||
{
|
||||
return $this->hasMany(ChatLink::class, 'class_id');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ class Teacher extends Model
|
||||
|
||||
protected $guarded = [
|
||||
'user_id',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
public function schoolClass()
|
||||
|
||||
@@ -6,10 +6,11 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Carbon\Carbon;
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory, Notifiable;
|
||||
use HasFactory, Notifiable, HasApiTokens;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
@@ -41,7 +42,7 @@ class User extends Authenticatable
|
||||
{
|
||||
foreach ($roles as $role)
|
||||
{
|
||||
if ($this->role->contains('name', $role))
|
||||
if ($this->role->name == $role)
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -58,11 +59,24 @@ class User extends Authenticatable
|
||||
return $this->hasOne(HeadTeacher::class);
|
||||
}
|
||||
|
||||
public function makeToken(bool $remember)
|
||||
public function teacher()
|
||||
{
|
||||
$token = $this->createToken(config('app.name'));
|
||||
$token->token->expires_at = $remember ? Carbon::now()->addMonth() : Carbon::now()->addDay();
|
||||
$token->token->save();
|
||||
return $token;
|
||||
return $this->hasOne(Teacher::class);
|
||||
}
|
||||
|
||||
public function student()
|
||||
{
|
||||
return $this->hasOne(Student::class);
|
||||
}
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->hasOne(Parentt::class);
|
||||
}
|
||||
|
||||
public function chatLinks()
|
||||
{
|
||||
return $this->hasMany(ChatLink::class, 'creator');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $policies = [
|
||||
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
|
||||
'App\Models\Model' => 'App\Policies\ModelPolicy',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
"guzzlehttp/guzzle": "^7.0.1",
|
||||
"laravel/framework": "^8.12",
|
||||
"laravel/passport": "^10.0",
|
||||
"laravel/tinker": "^2.5"
|
||||
"laravel/tinker": "^2.5",
|
||||
"lcobucci/jwt": "3.3.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"facade/ignition": "^2.5",
|
||||
|
||||
22
composer.lock
generated
22
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "d29a824fe1ba5e1a371425931ce19f0b",
|
||||
"content-hash": "3b9c799fff22949ca00970c9003aca42",
|
||||
"packages": [
|
||||
{
|
||||
"name": "asm89/stack-cors",
|
||||
@@ -1251,16 +1251,16 @@
|
||||
},
|
||||
{
|
||||
"name": "lcobucci/jwt",
|
||||
"version": "3.4.0",
|
||||
"version": "3.3.3",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/lcobucci/jwt.git",
|
||||
"reference": "320b9f05741b24acbbaf1106ed267ff3817fd74d"
|
||||
"reference": "c1123697f6a2ec29162b82f170dd4a491f524773"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/320b9f05741b24acbbaf1106ed267ff3817fd74d",
|
||||
"reference": "320b9f05741b24acbbaf1106ed267ff3817fd74d",
|
||||
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/c1123697f6a2ec29162b82f170dd4a491f524773",
|
||||
"reference": "c1123697f6a2ec29162b82f170dd4a491f524773",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -1275,9 +1275,6 @@
|
||||
"phpunit/phpunit": "^5.7 || ^7.3",
|
||||
"squizlabs/php_codesniffer": "~2.3"
|
||||
},
|
||||
"suggest": {
|
||||
"lcobucci/clock": "*"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
@@ -1287,12 +1284,7 @@
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lcobucci\\JWT\\": "src"
|
||||
},
|
||||
"files": [
|
||||
"compat/class-aliases.php",
|
||||
"compat/json-exception-polyfill.php",
|
||||
"compat/lcobucci-clock-polyfill.php"
|
||||
]
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
@@ -1320,7 +1312,7 @@
|
||||
"type": "patreon"
|
||||
}
|
||||
],
|
||||
"time": "2020-11-25T01:46:26+00:00"
|
||||
"time": "2020-08-20T13:22:28+00:00"
|
||||
},
|
||||
{
|
||||
"name": "league/commonmark",
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateChatLinksTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('chat_links', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('link');
|
||||
$table->unsignedInteger('class_id');
|
||||
$table->unsignedInteger('creator');
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('class_id')
|
||||
->references('id')->on('school_classes')
|
||||
->onDelete('cascade');
|
||||
$table->foreign('creator')
|
||||
->references('id')->on('users')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('chat_links');
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,14 @@ use Illuminate\Support\Facades\Route;
|
||||
Route::group(['prefix' => 'auth'], function () {
|
||||
Route::post('register', 'Auth\RegisterController');
|
||||
Route::post('login', 'Auth\LoginController');
|
||||
Route::post('logout', 'Auth\LogoutController');
|
||||
Route::post('logout', 'Auth\LogoutController')->middleware('auth:api');
|
||||
});
|
||||
|
||||
Route::apiResource('chat/links', 'ChatLinkController')->except(['show'])->middleware('auth:api'); // ссылки чата
|
||||
|
||||
Route::apiResource('headteachers', 'Users\HeadTeacherController');
|
||||
|
||||
Route::apiResource('teachers', 'Users\TeacherController');
|
||||
Route::apiResource('teachers', 'Users\TeacherController');//->middleware(['auth:api','role:headteacher|teacher']);
|
||||
Route::get('teacher/{teacher}/classes', 'Users\TeacherController@getClasses'); //получить классы у которых ведет учитель
|
||||
|
||||
Route::apiResource('students', 'Users\StudentController');
|
||||
|
||||
Reference in New Issue
Block a user