This commit is contained in:
glovetleong
2021-06-17 09:53:04 +08:00
commit 8fa80e4a1a
2382 changed files with 196486 additions and 0 deletions
+148
View File
@@ -0,0 +1,148 @@
<?php
namespace App\Http\Rules\Admin;
use App\Models\Country;
use App\Models\Role;
use App\Models\Permission;
use App\Models\Currency;
use App\Models\Language;
use App\Models\MediaPropertyType;
use App\Models\MimeType;
use App\Http\Helpers\Hasher;
use Illuminate\Contracts\Validation\Rule;
class ExistCheck implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
private $type;
private $should_not;
public function __construct($type = '', $should_not = false)
{
$this->type = $type;
$this->should_not = $should_not;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->attribute = $attribute;
$value = is_array($value) ? $value : [$value];
if ($this->type == 'country') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('countries', $row);
$data = Country::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'role') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('roles', $row);
$data = Role::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'permission') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('permissions', $row);
$data = Permission::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'currency') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('currencies', $row);
$data = Currency::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'language') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('languages', $row);
$data = Language::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'media_property_type') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('media_property_types', $row);
$data = MediaPropertyType::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'mime_type') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('mime_types', $row);
$data = MimeType::find($id);
if (!$data) {
return false;
}
}
}
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
if (!$this->should_not) {
return config('error_code.exist_error');
}
else {
return config('error_code.unique_error');
}
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace App\Http\Rules\Admin;
use Illuminate\Contracts\Validation\Rule;
class Image64Check implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->attribute = $attribute;
$accept_mime_type = ['image/gif', 'image/png', 'image/jpeg'];
$f = finfo_open();
$mime_type = finfo_file($f, $value, FILEINFO_MIME_TYPE);
if (!in_array($mime_type, $accept_mime_type)) {
return false;
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return config('error_code.img_error');
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace App\Http\Rules\Admin;
use Auth;
use App\Models\Admin;
use Illuminate\Contracts\Validation\Rule;
class LoginRule implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
protected $password;
public function __construct($password = '')
{
$this->password = $password;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$email = $value;
$password = $this->password;
if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password])) {
$data = Admin::
where('email', $email)
->whereIn('status', [1])
->count();
if (!$data) {
return false;
}
return true;
}
return false;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return config('error_code.login_error');
}
}
+121
View File
@@ -0,0 +1,121 @@
<?php
namespace App\Http\Rules\User;
use Auth;
use App\Models\User;
use App\Models\UserSocialAccount;
use Illuminate\Contracts\Validation\Rule;
use OTPHP\TOTP;
class LoginRule implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
protected $type;
protected $password;
protected $totp;
protected $pending_error;
public function __construct(
$type = '',
$password = '',
$totp = '',
$pending_error = false
)
{
$this->type = $type;
$this->password = $password;
$this->totp = $totp;
$this->pending_error = $pending_error;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if ($this->type == 'manual') {
$email = $value;
$password = $this->password;
if (Auth::guard('user')->attempt(['email' => $email, 'password' => $password])) {
$data = User::
where('email', $email)
->whereIn('status', [1, 9])
->count();
if (!$data) {
return false;
}
return true;
}
}
// if ($this->type == 'social') {
// $social_media_type_id = $value;
// $provider_id = $this->password;
// $data = UserSocialAccount::
// where('social_media_type_id', $social_media_type_id)
// ->where('provider_id', $provider_id)
// ->whereHas('user', function($q) {
// $q->whereIn('status', [1]);
// })
// ->count();
// if (!$data) {
// return false;
// }
// return true;
// }
// if ($this->type == 'totp') {
// $social_media_type_id = $value;
// $provider_id = $this->password;
// $data = UserSocialAccount::
// where('social_media_type_id', $social_media_type_id)
// ->where('provider_id', $provider_id)
// ->count();
// if (!$data) {
// return false;
// }
// $totp = new TOTP(
// $social_media_type_id . $provider_id,
// env('TOTP_SECRET'),
// 100,
// 'sha512',
// 8
// );
// if (!$totp->verify($this->totp)) {
// return false;
// }
// return true;
// }
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
if (!$this->pending_error) {
return config('error_code.login_error');
}
else {
return config('error_code.pending_error');
}
}
}