nanosanteservice/app/Rules/PasswordValidation.php

88 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Rules;
use App\Exceptions\AppException;
use App\Models\AgentPlus;
use App\Models\NhNetworksConfig;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class PasswordValidation implements ImplicitRule, DataAwareRule
{
/**
* All of the data under validation.
*
* @var array
*/
protected $data = [];
private $network_id;
private $type;
private $user;
public function __construct($network_id, $type = 'agent', $user = null)
{
$this->network_id = $network_id;
$this->type = $type;
$this->user = $user;
}
/**
* Set the data under validation.
*
* @param array $data
* @return $this
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
* @throws AppException
*/
public function passes($attribute, $value)
{
$config = NhNetworksConfig::where('network_id', $this->network_id)->first();
if (isset($config) && $config->password_validation == 'MAX') {
$validator = Validator::make($this->data, [
'password' => 'required|string'
]);
if ($validator->fails()) {
throw new AppException(trans('validation.required', ['attribute' => 'password']), 422);
}
if ($this->type == 'agent') {
$network_agent_id = $this->data['network_agent_id'] ?? $this->data['issuer_network_agent_id'];
$agent = AgentPlus::where('network_agent_id', $network_agent_id)->first();
return checkPassword($value, $agent->encrypted_password, $agent->salt);
} else {
return checkPassword($value, $this->user->encrypted_password, $this->user->salt);
}
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return trans('messages.incorrect_user_password');
}
}