2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Preference.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* 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 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
2016-05-20 01:57:45 -05:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Models;
|
2015-02-05 21:52:16 -06:00
|
|
|
|
2015-05-23 01:46:46 -05:00
|
|
|
use Crypt;
|
2016-06-27 09:36:28 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
2015-02-05 21:52:16 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-08-05 14:50:49 -05:00
|
|
|
use Log;
|
2016-08-25 23:44:24 -05:00
|
|
|
|
2016-11-18 13:06:08 -06:00
|
|
|
/**
|
|
|
|
* Class Preference
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Models
|
|
|
|
*/
|
2015-02-05 22:04:06 -06:00
|
|
|
class Preference extends Model
|
|
|
|
{
|
2015-02-05 21:52:16 -06:00
|
|
|
|
2016-01-28 15:06:16 -06:00
|
|
|
protected $dates = ['created_at', 'updated_at'];
|
2016-10-06 22:44:21 -05:00
|
|
|
protected $fillable = ['user_id', 'data', 'name', 'data'];
|
2016-01-15 16:12:52 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
2016-07-24 11:47:55 -05:00
|
|
|
* @throws FireflyException
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2015-02-05 22:14:27 -06:00
|
|
|
public function getDataAttribute($value)
|
|
|
|
{
|
2016-06-27 09:36:28 -05:00
|
|
|
try {
|
|
|
|
$data = Crypt::decrypt($value);
|
|
|
|
} catch (DecryptException $e) {
|
2016-09-20 00:57:04 -05:00
|
|
|
Log::error('Could not decrypt preference.', ['id' => $this->id, 'name' => $this->name, 'data' => $value]);
|
2016-10-25 23:41:50 -05:00
|
|
|
throw new FireflyException(
|
|
|
|
sprintf('Could not decrypt preference #%d. If this error persists, please run "php artisan cache:clear" on the command line.', $this->id)
|
|
|
|
);
|
2016-06-27 09:36:28 -05:00
|
|
|
}
|
2015-05-23 01:46:46 -05:00
|
|
|
|
2016-10-25 23:41:50 -05:00
|
|
|
|
2015-05-23 01:46:46 -05:00
|
|
|
return json_decode($data);
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*/
|
2015-02-05 22:14:27 -06:00
|
|
|
public function setDataAttribute($value)
|
|
|
|
{
|
2016-06-23 05:08:14 -05:00
|
|
|
$this->attributes['data'] = Crypt::encrypt(json_encode($value));
|
2015-05-23 01:46:46 -05:00
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-02-05 22:14:27 -06:00
|
|
|
public function user()
|
|
|
|
{
|
2015-02-05 22:35:00 -06:00
|
|
|
return $this->belongsTo('FireflyIII\User');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
2015-02-05 21:52:16 -06:00
|
|
|
|
|
|
|
}
|