2015-02-07 18:15:15 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Validation;
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
use Auth;
|
2015-03-26 12:05:23 -05:00
|
|
|
use Config;
|
2015-03-30 13:16:33 -05:00
|
|
|
use Crypt;
|
2015-02-07 18:15:15 -06:00
|
|
|
use DB;
|
2015-03-30 13:08:27 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2015-03-26 12:05:23 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2015-06-27 01:06:24 -05:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
2015-06-05 05:18:20 -05:00
|
|
|
use FireflyIII\User;
|
2015-06-05 05:34:45 -05:00
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
2015-02-11 00:35:10 -06:00
|
|
|
use Illuminate\Validation\Validator;
|
2015-05-01 11:44:49 -05:00
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2015-02-07 18:15:15 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* Class FireflyValidator
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Validation
|
|
|
|
*/
|
2015-02-07 18:15:15 -06:00
|
|
|
class FireflyValidator extends Validator
|
|
|
|
{
|
|
|
|
|
2015-05-01 11:44:49 -05:00
|
|
|
/**
|
|
|
|
* @param TranslatorInterface $translator
|
|
|
|
* @param array $data
|
|
|
|
* @param array $rules
|
|
|
|
* @param array $messages
|
|
|
|
* @param array $customAttributes
|
2015-05-17 03:10:58 -05:00
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
2015-05-01 11:44:49 -05:00
|
|
|
*/
|
|
|
|
public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
|
|
|
|
{
|
2015-05-17 03:10:58 -05:00
|
|
|
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
|
2015-05-01 11:44:49 -05:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateBelongsToUser($attribute, $value, $parameters)
|
|
|
|
{
|
2015-03-30 13:08:27 -05:00
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
$count = DB::table($parameters[0])->where('user_id', Auth::user()->id)->where('id', $value)->count();
|
|
|
|
if ($count == 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:05:23 -05:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
2015-07-06 15:23:34 -05:00
|
|
|
*
|
|
|
|
* @return bool
|
2015-07-03 05:51:14 -05:00
|
|
|
*/
|
2015-07-06 09:42:19 -05:00
|
|
|
public function validateIban($attribute, $value)
|
2015-07-03 05:51:14 -05:00
|
|
|
{
|
2015-08-16 13:26:11 -05:00
|
|
|
if (!is_string($value) || is_null($value) || strlen($value) < 6) {
|
2015-07-06 18:07:19 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-03 05:51:14 -05:00
|
|
|
|
|
|
|
$value = strtoupper($value);
|
|
|
|
|
|
|
|
$search = [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
|
|
|
|
$replace = ['', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31',
|
|
|
|
'32', '33', '34', '35'];
|
|
|
|
|
|
|
|
// take
|
|
|
|
$first = substr($value, 0, 4);
|
|
|
|
$last = substr($value, 4);
|
|
|
|
$iban = $last . $first;
|
|
|
|
$iban = str_replace($search, $replace, $iban);
|
|
|
|
$checksum = bcmod($iban, '97');
|
|
|
|
|
|
|
|
return (intval($checksum) === 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
2015-03-26 12:05:23 -05:00
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateUniqueAccountForUser($attribute, $value, $parameters)
|
|
|
|
{
|
2015-06-05 05:18:20 -05:00
|
|
|
// because a user does not have to be logged in (tests and what-not).
|
|
|
|
if (!Auth::check()) {
|
|
|
|
return $this->validateAccountAnonymously();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->data['what'])) {
|
|
|
|
return $this->validateByAccountTypeString($value, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->data['account_type_id'])) {
|
|
|
|
return $this->validateByAccountTypeId($value, $parameters);
|
|
|
|
}
|
2015-06-29 00:22:51 -05:00
|
|
|
if (isset($this->data['id'])) {
|
|
|
|
return $this->validateByAccountId($value);
|
2015-06-28 14:13:08 -05:00
|
|
|
}
|
|
|
|
|
2015-03-27 03:24:26 -05:00
|
|
|
|
2015-06-05 05:34:45 -05:00
|
|
|
return false;
|
2015-06-05 05:18:20 -05:00
|
|
|
}
|
2015-03-30 13:16:33 -05:00
|
|
|
|
2015-06-05 05:18:20 -05:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function validateAccountAnonymously()
|
|
|
|
{
|
|
|
|
if (!isset($this->data['user_id'])) {
|
2015-03-30 13:08:27 -05:00
|
|
|
return false;
|
2015-03-26 12:05:23 -05:00
|
|
|
}
|
2015-03-27 01:20:32 -05:00
|
|
|
|
2015-06-05 05:18:20 -05:00
|
|
|
$user = User::find($this->data['user_id']);
|
|
|
|
$type = AccountType::find($this->data['account_type_id'])->first();
|
2015-06-05 05:48:58 -05:00
|
|
|
$value = $this->tryDecrypt($this->data['name']);
|
2015-06-05 05:18:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
$set = $user->accounts()->where('account_type_id', $type->id)->get();
|
|
|
|
/** @var Account $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
if ($entry->name == $value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-05 05:48:58 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function tryDecrypt($value)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$value = Crypt::decrypt($value);
|
|
|
|
} catch (DecryptException $e) {
|
|
|
|
// do not care.
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-06-05 05:18:20 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function validateByAccountTypeString($value, $parameters)
|
|
|
|
{
|
|
|
|
$search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']);
|
|
|
|
$type = AccountType::whereType($search)->first();
|
|
|
|
$ignore = isset($parameters[0]) ? intval($parameters[0]) : 0;
|
|
|
|
|
|
|
|
$set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
|
|
|
|
/** @var Account $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
if ($entry->name == $value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function validateByAccountTypeId($value, $parameters)
|
|
|
|
{
|
|
|
|
$type = AccountType::find($this->data['account_type_id'])->first();
|
|
|
|
$ignore = isset($parameters[0]) ? intval($parameters[0]) : 0;
|
2015-06-05 05:48:58 -05:00
|
|
|
$value = $this->tryDecrypt($value);
|
2015-06-05 05:18:20 -05:00
|
|
|
|
|
|
|
$set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
|
2015-03-30 13:08:27 -05:00
|
|
|
/** @var Account $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
if ($entry->name == $value) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-26 12:05:23 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 13:08:27 -05:00
|
|
|
return true;
|
2015-03-26 12:05:23 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:13:08 -05:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return bool
|
2015-07-06 15:23:34 -05:00
|
|
|
* @internal param $parameters
|
|
|
|
*
|
2015-06-28 14:13:08 -05:00
|
|
|
*/
|
2015-06-29 00:22:51 -05:00
|
|
|
protected function validateByAccountId($value)
|
2015-06-28 14:13:08 -05:00
|
|
|
{
|
|
|
|
/** @var Account $existingAccount */
|
|
|
|
$existingAccount = Account::find($this->data['id']);
|
|
|
|
|
|
|
|
$type = $existingAccount->accountType;
|
|
|
|
$ignore = $existingAccount->id;
|
|
|
|
$value = $this->tryDecrypt($value);
|
|
|
|
|
|
|
|
$set = Auth::user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
|
|
|
|
/** @var Account $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
if ($entry->name == $value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-02-07 18:15:15 -06:00
|
|
|
public function validateUniqueForUser($attribute, $value, $parameters)
|
|
|
|
{
|
2015-03-06 08:12:07 -06:00
|
|
|
$query = DB::table($parameters[0])->where($parameters[1], $value);
|
2015-03-26 12:05:23 -05:00
|
|
|
$query->where('user_id', Auth::user()->id);
|
2015-05-26 12:38:52 -05:00
|
|
|
if (isset($parameters[2])) {
|
2015-03-06 08:12:07 -06:00
|
|
|
$query->where('id', '!=', $parameters[2]);
|
|
|
|
}
|
|
|
|
$count = $query->count();
|
2015-02-11 00:35:10 -06:00
|
|
|
if ($count == 0) {
|
2015-02-07 18:15:15 -06:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-11 00:35:10 -06:00
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2015-03-27 14:20:52 -05:00
|
|
|
|
2015-03-31 07:16:25 -05:00
|
|
|
/**
|
|
|
|
* Validate an object and its unicity. Checks for encryption / encrypted values as well.
|
|
|
|
*
|
|
|
|
* parameter 0: the table
|
|
|
|
* parameter 1: the field
|
2015-06-05 05:34:45 -05:00
|
|
|
* parameter 2: an id to ignore (when editing)
|
2015-03-31 07:16:25 -05:00
|
|
|
*
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateUniqueObjectForUser($attribute, $value, $parameters)
|
|
|
|
{
|
2015-06-05 05:48:58 -05:00
|
|
|
$value = $this->tryDecrypt($value);
|
2015-06-05 05:34:45 -05:00
|
|
|
// exclude?
|
|
|
|
$table = $parameters[0];
|
|
|
|
$field = $parameters[1];
|
2015-06-07 02:09:27 -05:00
|
|
|
$exclude = isset($parameters[2]) ? intval($parameters[2]) : 0;
|
2015-03-31 07:16:25 -05:00
|
|
|
|
2015-06-05 05:34:45 -05:00
|
|
|
// get entries from table
|
2015-10-19 08:16:30 -05:00
|
|
|
$set = DB::table($table)->where('user_id', Auth::user()->id)
|
2015-12-18 09:38:50 -06:00
|
|
|
->where('id', '!=', $exclude)->get([$field]);
|
2015-03-31 07:16:25 -05:00
|
|
|
|
|
|
|
foreach ($set as $entry) {
|
2015-06-05 05:48:58 -05:00
|
|
|
$fieldValue = $this->tryDecrypt($entry->$field);
|
2015-06-05 05:34:45 -05:00
|
|
|
|
|
|
|
if ($fieldValue === $value) {
|
2015-03-31 07:16:25 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-27 14:20:52 -05:00
|
|
|
/**
|
|
|
|
* @param $attribute
|
|
|
|
* @param $value
|
|
|
|
* @param $parameters
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateUniquePiggyBankForUser($attribute, $value, $parameters)
|
|
|
|
{
|
2015-03-31 07:16:25 -05:00
|
|
|
$exclude = isset($parameters[0]) ? $parameters[0] : null;
|
|
|
|
$query = DB::table('piggy_banks');
|
2015-10-19 08:21:17 -05:00
|
|
|
$query->whereNull('piggy_banks.deleted_at');
|
2015-03-27 14:20:52 -05:00
|
|
|
$query->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id');
|
|
|
|
$query->where('accounts.user_id', Auth::user()->id);
|
2015-03-31 07:16:25 -05:00
|
|
|
if (!is_null($exclude)) {
|
|
|
|
$query->where('piggy_banks.id', '!=', $exclude);
|
2015-03-27 14:20:52 -05:00
|
|
|
}
|
2015-03-31 07:16:25 -05:00
|
|
|
$set = $query->get(['piggy_banks.*']);
|
|
|
|
|
2015-06-27 01:06:24 -05:00
|
|
|
/** @var PiggyBank $entry */
|
2015-04-07 11:26:14 -05:00
|
|
|
foreach ($set as $entry) {
|
2015-06-06 16:09:12 -05:00
|
|
|
$fieldValue = $this->tryDecrypt($entry->name);
|
2015-06-05 05:48:58 -05:00
|
|
|
if ($fieldValue == $value) {
|
2015-03-31 07:16:25 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-27 14:20:52 -05:00
|
|
|
}
|
|
|
|
|
2015-03-31 07:16:25 -05:00
|
|
|
return true;
|
2015-03-27 14:20:52 -05:00
|
|
|
|
|
|
|
}
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|
|
|
|
|