You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.7 KiB
PHP
177 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
use App\Helper\FileHelper;
|
|
use App\Helper\JSONResponse;
|
|
use App\Helper\Traits\Models\CanMultiOrderBy;
|
|
use App\Helper\Traits\Models\CanMultiSearch;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable;
|
|
use CanMultiSearch;
|
|
use CanMultiOrderBy;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'password',
|
|
'username',
|
|
'is_active'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
|
|
public static function upsertFromRequest(Request $request) {
|
|
$request->validate([
|
|
'id' => 'nullable|integer|exists:App\Models\User,id',
|
|
'name' => 'required|string',
|
|
'username' => 'required|string',
|
|
'email' => 'required|email',
|
|
'password' => 'required_without:id|string|min:8',
|
|
'is_active' => 'required_with:id|in:true,false',
|
|
], [
|
|
'password' => ['required_with' => 'The password field is required.'],
|
|
'is_active' => ['required_with' => 'The is active field is required.']
|
|
]);
|
|
|
|
try {
|
|
// try to upsert data
|
|
DB::beginTransaction();
|
|
$user = null;
|
|
if(!$request->id) {
|
|
$user = new User();
|
|
$user->is_active = $request->is_active;
|
|
$user->password = Hash::make($request->password);
|
|
}
|
|
else $user = User::findOrFail($request->id);
|
|
|
|
$user->name = $request->email;
|
|
$user->email = $request->email;
|
|
$user->username = $request->username;
|
|
$user->checkUniqueFieldBeforeExecuteDB();
|
|
$user->save();
|
|
|
|
// renew data;
|
|
DB::commit();
|
|
return JSONResponse::Success();
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
public static function deleteFromRequest(Request $request) {
|
|
$request->validate(['id' => 'required|integer|exists:App\Models\User,id']);
|
|
try {
|
|
DB::beginTransaction();
|
|
$user = User::findOrFail($request->id);
|
|
if($user->is_selected) throw new \Exception("Cannot delete video when 'is Selected' is true");
|
|
|
|
$oldDbFile = $user->file;
|
|
$user->delete();
|
|
|
|
if($oldDbFile) self::deleteFile($oldDbFile);
|
|
DB::commit();
|
|
return JSONResponse::Success();
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
throw $th;
|
|
}
|
|
|
|
}
|
|
|
|
public static function changeStatusFromRequest(Request $request) {
|
|
$request->validate(['id' => 'required|integer|exists:App\Models\User,id']);
|
|
|
|
$user = User::findOrFail($request->id);
|
|
$user->preventChangeForSelfUser($request->user());
|
|
$user->is_active = !$user->is_active;
|
|
$user->save();
|
|
return JSONResponse::Success();
|
|
}
|
|
|
|
public function preventChangeForSelfUser(User $currentUser) {
|
|
if($currentUser->id == $this->id) throw new \Exception("You cannot 'delete' / 'change status' your own user");
|
|
}
|
|
|
|
public function checkUniqueFieldBeforeExecuteDB() {
|
|
// check email
|
|
$isUsernameExist = User::where('username', $this->username)
|
|
->when($this->id, function(Builder $q, $userId) {
|
|
$q->where('id', '!=', $userId);
|
|
})->first();
|
|
if($isUsernameExist) throw new \Exception("Username '" . $this->username . "' has already used by another user");
|
|
|
|
// check email
|
|
$isEmailExist = User::where('email', $this->email)
|
|
->when($this->id, function(Builder $q, $userId) {
|
|
$q->where('id', '!=', $userId);
|
|
})->first();
|
|
if($isEmailExist) throw new \Exception("Email '" . $this->email . "' has already used by another user");
|
|
}
|
|
|
|
public static function changePasswordFromRequest(Request $request) {
|
|
$request->validate([
|
|
'id' => 'required|integer|exists:App\Models\User,id',
|
|
'newPassword' => 'required|string|min:8',
|
|
'confirmNewPassword' => 'required|string|min:8',
|
|
]);
|
|
|
|
if($request->newPassword != $request->confirmNewPassword) {
|
|
throw new \Exception("New Password & Confirm New Pasword are not same");
|
|
}
|
|
|
|
$user = User::findOrFail($request->id);
|
|
$user->password = Hash::make($request->newPassword);
|
|
$user->save();
|
|
return JSONResponse::Success();
|
|
}
|
|
|
|
public function getObjSession($currentAccessToken) {
|
|
return [
|
|
'username' => $this->username,
|
|
'email' => $this->email,
|
|
'token' => $currentAccessToken,
|
|
'allowedFileExtension' => [
|
|
'video' => FileHelper::convertToStrJsValidation(FileHelper::$allowedVideoExtensions),
|
|
'apk' => FileHelper::convertToStrJsValidation(FileHelper::$allowedApkExtensions)
|
|
]
|
|
];
|
|
}
|
|
}
|