Cleanup another validator [skip ci]

This commit is contained in:
James Cole 2015-06-05 12:34:45 +02:00
parent e1aa63487a
commit 8d8308e557
7 changed files with 33 additions and 74 deletions

View File

@ -48,8 +48,8 @@ class BillFormRequest extends Request
*/
public function rules()
{
$nameRule = 'required|between:1,255|uniqueObjectForUser:bills,name,name_encrypted';
$matchRule = 'required|between:1,255|uniqueObjectForUser:bills,match,match_encrypted';
$nameRule = 'required|between:1,255|uniqueObjectForUser:bills,name';
$matchRule = 'required|between:1,255|uniqueObjectForUser:bills,match';
if (intval(Input::get('id')) > 0) {
$nameRule .= ',' . intval(Input::get('id'));
$matchRule .= ',' . intval(Input::get('id'));

View File

@ -29,9 +29,9 @@ class BudgetFormRequest extends Request
public function rules()
{
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name,encrypted';
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name';
if (Budget::find(Input::get('id'))) {
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,name,encrypted,' . intval(Input::get('id'));
$nameRule = 'required|between:1,100|uniqueObjectForUser:budgets,' . intval(Input::get('id'));
}
return [

View File

@ -29,9 +29,9 @@ class CategoryFormRequest extends Request
public function rules()
{
$nameRule = 'required|between:1,100|uniqueObjectForUser:categories,name,encrypted';
$nameRule = 'required|between:1,100|uniqueObjectForUser:categories,name';
if (Category::find(Input::get('id'))) {
$nameRule = 'required|between:1,100|uniqueObjectForUser:categories,name,encrypted,' . intval(Input::get('id'));
$nameRule = 'required|between:1,100|uniqueObjectForUser:categories,name,' . intval(Input::get('id'));
}
return [

View File

@ -28,10 +28,10 @@ class TagFormRequest extends Request
public function rules()
{
$idRule = '';
$tagRule = 'required|min:1|uniqueObjectForUser:tags,tag,TRUE';
$tagRule = 'required|min:1|uniqueObjectForUser:tags,tag';
if (Tag::find(Input::get('id'))) {
$idRule = 'belongsToUser:tags';
$tagRule = 'required|min:1|uniqueObjectForUser:tags,tag,TRUE,' . Input::get('id');
$tagRule = 'required|min:1|uniqueObjectForUser:tags,tag,' . Input::get('id');
}
return [

View File

@ -55,6 +55,8 @@ class Bill extends Model
protected $hidden = ['amount_min_encrypted', 'amount_max_encrypted', 'name_encrypted', 'match_encrypted'];
/**
* @return array
*/

View File

@ -44,7 +44,7 @@ class Tag extends Model
protected $fillable = ['user_id', 'tag', 'date', 'description', 'longitude', 'latitude', 'zoomLevel', 'tagMode'];
protected $rules
= [
'tag' => 'required|min:1|uniqueObjectForUser:tags,tag,TRUE',
'tag' => 'required|min:1|uniqueObjectForUser:tags,tag',
'description' => 'min:1',
'date' => 'date',
'latitude' => 'numeric|min:-90|max:90',

View File

@ -10,6 +10,7 @@ use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\User;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Validation\Validator;
use Navigation;
use Symfony\Component\Translation\TranslatorInterface;
@ -105,44 +106,7 @@ class FireflyValidator extends Validator
return $this->validateByAccountTypeId($value, $parameters);
}
var_dump($attribute);
var_dump($value);
var_dump($parameters);
var_dump($this->data);
exit;
// try to determin type of account:
if (!empty($this->data['what'])) {
$search = Config::get('firefly.accountTypeByIdentifier.' . $this->data['what']);
$type = AccountType::whereType($search)->first();
} else {
$type = AccountType::find($this->data['account_type_id']);
}
// ignore itself, if parameter is given:
if (isset($parameters[0])) {
$ignoreId = $parameters[0];
} else {
$ignoreId = 0;
}
// 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;
}
}
return true;
return false;
}
/**
@ -254,8 +218,7 @@ class FireflyValidator extends Validator
*
* 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)
* parameter 2: an id to ignore (when editing)
*
* @param $attribute
* @param $value
@ -265,37 +228,31 @@ class FireflyValidator extends Validator
*/
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;
// try to decrypt value. if it fails, not a problem:
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
// do not care.
}
if (is_null(Auth::user())) {
// user is not logged in.. weird.
return true;
} else {
$query = DB::table($table)->where('user_id', Auth::user()->id);
}
// exclude?
$table = $parameters[0];
$field = $parameters[1];
$exclude = isset($parameters[3]) ? intval($parameters[3]) : 0;
// get entries from table
$set = DB::table($table)->where('user_id', Auth::user()->id)->where('id', '!=', $exclude)->get([$field]);
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;
$fieldValue = $entry->$field;
// try to decrypt:
try {
$fieldValue = Crypt::decrypt($entry->$field);
} catch (DecryptException $e) {
// dont care
}
$checkValue = $isEncrypted ? Crypt::decrypt($entry->$field) : $entry->$field;
if ($checkValue == $value) {
if ($fieldValue === $value) {
return false;
}
}