firefly-iii/app/Validation/FireflyValidator.php

337 lines
9.2 KiB
PHP
Raw Normal View History

<?php
namespace FireflyIII\Validation;
2015-02-24 15:53:38 -06:00
use Auth;
use Carbon\Carbon;
2015-03-26 12:05:23 -05:00
use Config;
use Crypt;
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-05 05:18:20 -05:00
use FireflyIII\User;
2015-02-11 00:35:10 -06:00
use Illuminate\Validation\Validator;
use Navigation;
2015-05-01 11:44:49 -05:00
use Symfony\Component\Translation\TranslatorInterface;
2015-02-11 00:35:10 -06:00
/**
* Class FireflyValidator
*
* @package FireflyIII\Validation
*/
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
/**
* @return bool
*/
2015-05-31 13:52:20 -05:00
public function validatePiggyBankReminder()
{
$array = $this->data;
// no reminder? dont care.
if (!isset($array['remind_me'])) {
return true;
}
// get or set start date & target date:
$startDate = isset($array['startdate']) ? new Carbon($array['startdate']) : new Carbon;
$targetDate = isset($array['targetdate']) && strlen($array['targetdate']) > 0 ? new Carbon($array['targetdate']) : null;
// target date is null? reminder period is always good.
if ($array['remind_me'] == '1' && is_null($targetDate)) {
return true;
}
2015-03-26 12:05:23 -05:00
$nextReminder = Navigation::addPeriod($startDate, $array['reminder'], 0);
// reminder is beyond target?
2015-03-26 12:05:23 -05:00
if ($nextReminder > $targetDate) {
return false;
}
2015-03-26 12:05:23 -05:00
return true;
}
2015-03-26 12:05:23 -05:00
/**
* @param $attribute
* @param $value
* @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-03-27 03:24:26 -05:00
2015-06-05 05:18:20 -05:00
var_dump($attribute);
var_dump($value);
var_dump($parameters);
var_dump($this->data);
2015-06-05 05:18:20 -05:00
exit;
// try to determin type of account:
if (!empty($this->data['what'])) {
2015-03-30 13:08:27 -05:00
$search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']);
$type = AccountType::whereType($search)->first();
2015-06-05 05:18:20 -05:00
} else {
$type = AccountType::find($this->data['account_type_id']);
}
2015-06-05 05:18:20 -05:00
// ignore itself, if parameter is given:
if (isset($parameters[0])) {
$ignoreId = $parameters[0];
} else {
$ignoreId = 0;
2015-03-26 12:05:23 -05:00
}
2015-06-05 05:18:20 -05:00
// reset to basic check, see what happens:
$userId = Auth::user()->id;
$ignoreId = intval($this->data['id']);
$set = Account::where('account_type_id', $type->id)->where('id', '!=', $ignoreId)->where('user_id', $userId)->get();
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->name == $value) {
return false;
}
}
2015-06-05 05:18:20 -05:00
return true;
2015-06-05 05:18:20 -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-06-05 05:18:20 -05:00
$user = User::find($this->data['user_id']);
$type = AccountType::find($this->data['account_type_id'])->first();
$value = $this->data['name'];
// decrypt if necessary
if (intval($this->data['encrypted']) === 1) {
$value = Crypt::decrypt($this->data['name']);
}
$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;
}
/**
* @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;
// if is encrypted, decrypt:
if (intval($this->data['encrypted']) === 1) {
$value = Crypt::decrypt($value);
}
$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-02-11 00:35:10 -06:00
/**
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniqueForUser($attribute, $value, $parameters)
{
$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])) {
$query->where('id', '!=', $parameters[2]);
}
$count = $query->count();
2015-02-11 00:35:10 -06:00
if ($count == 0) {
return true;
}
2015-02-11 00:35:10 -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
* parameter 2: the encrypted / not encrypted boolean. Defaults to "encrypted".
* parameter 3: an id to ignore (when editing)
*
* @param $attribute
* @param $value
* @param $parameters
*
* @return bool
*/
public function validateUniqueObjectForUser($attribute, $value, $parameters)
{
$table = $parameters[0];
$field = $parameters[1];
$encrypted = isset($parameters[2]) ? $parameters[2] : 'encrypted';
$exclude = isset($parameters[3]) ? $parameters[3] : null;
$alwaysEncrypted = false;
if ($encrypted == 'TRUE') {
$alwaysEncrypted = true;
}
2015-03-31 07:16:25 -05:00
2015-05-05 00:51:02 -05:00
if (is_null(Auth::user())) {
2015-05-03 08:00:39 -05:00
// user is not logged in.. weird.
return true;
} else {
$query = DB::table($table)->where('user_id', Auth::user()->id);
}
2015-03-31 07:16:25 -05:00
if (!is_null($exclude)) {
$query->where('id', '!=', $exclude);
}
$set = $query->get();
foreach ($set as $entry) {
if (!$alwaysEncrypted) {
$isEncrypted = intval($entry->$encrypted) == 1 ? true : false;
} else {
$isEncrypted = true;
}
$checkValue = $isEncrypted ? Crypt::decrypt($entry->$field) : $entry->$field;
2015-03-31 07:16:25 -05:00
if ($checkValue == $value) {
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-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-04-07 11:26:14 -05:00
foreach ($set as $entry) {
2015-03-31 07:16:25 -05:00
$isEncrypted = intval($entry->encrypted) == 1 ? true : false;
2015-04-07 11:26:14 -05:00
$checkValue = $isEncrypted ? Crypt::decrypt($entry->name) : $entry->name;
if ($checkValue == $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
}
}