Files
firefly-iii/app/Models/Preference.php

84 lines
2.7 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* Preference.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 08:57:45 +02:00
declare(strict_types = 1);
namespace FireflyIII\Models;
2015-02-06 04:52:16 +01:00
2015-05-23 08:46:46 +02:00
use Crypt;
2016-06-27 16:36:28 +02:00
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Contracts\Encryption\DecryptException;
2015-02-06 04:52:16 +01:00
use Illuminate\Database\Eloquent\Model;
2016-08-05 21:50:49 +02:00
use Log;
2016-08-26 06:44:24 +02:00
2015-02-11 07:35:10 +01:00
/**
2016-01-01 21:49:27 +01:00
* FireflyIII\Models\Preference
2015-02-11 07:35:10 +01:00
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $user_id
* @property string $name
* @property string $name_encrypted
* @property string $data
* @property-read \FireflyIII\User $user
2016-03-12 07:36:23 +01:00
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereName($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereNameEncrypted($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereData($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Preference whereDataEncrypted($value)
* @mixin \Eloquent
2015-02-11 07:35:10 +01:00
*/
2015-02-06 05:04:06 +01:00
class Preference extends Model
{
2015-02-06 04:52:16 +01:00
protected $dates = ['created_at', 'updated_at'];
2016-09-20 07:57:04 +02:00
protected $fillable = ['user_id', 'data', 'name','data'];
2016-01-15 23:12:52 +01:00
2015-02-11 07:35:10 +01:00
/**
* @param $value
*
* @return mixed
* @throws FireflyException
2015-02-11 07:35:10 +01:00
*/
2015-02-06 05:14:27 +01:00
public function getDataAttribute($value)
{
2016-06-27 16:36:28 +02:00
try {
$data = Crypt::decrypt($value);
} catch (DecryptException $e) {
2016-09-20 07:57:04 +02:00
Log::error('Could not decrypt preference.', ['id' => $this->id, 'name' => $this->name, 'data' => $value]);
2016-06-27 16:36:28 +02:00
throw new FireflyException('Could not decrypt preference #' . $this->id . '.');
}
2015-05-23 08:46:46 +02:00
return json_decode($data);
2015-02-06 05:14:27 +01:00
}
2015-02-11 07:35:10 +01:00
/**
* @param $value
*/
2015-02-06 05:14:27 +01:00
public function setDataAttribute($value)
{
2016-06-23 12:08:14 +02:00
$this->attributes['data'] = Crypt::encrypt(json_encode($value));
2015-05-23 08:46:46 +02:00
}
2015-02-11 07:35:10 +01:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-06 05:14:27 +01:00
public function user()
{
2015-02-06 05:35:00 +01:00
return $this->belongsTo('FireflyIII\User');
2015-02-06 05:14:27 +01:00
}
2015-02-06 04:52:16 +01:00
}