2018-08-10 17:14:09 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
|
|
|
* Copyright (C) DevCode s.n.c.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-08-10 17:14:09 +02:00
|
|
|
|
|
|
|
namespace Models;
|
|
|
|
|
2020-09-22 20:28:37 +02:00
|
|
|
use Common\SimpleModelTrait;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-07-18 18:33:56 +02:00
|
|
|
use Intervention\Image\ImageManagerStatic;
|
2019-07-19 15:23:00 +02:00
|
|
|
use Modules\Anagrafiche\Anagrafica;
|
2018-08-10 17:14:09 +02:00
|
|
|
|
|
|
|
class User extends Model
|
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
use SimpleModelTrait;
|
|
|
|
|
2018-08-10 17:14:09 +02:00
|
|
|
protected $table = 'zz_users';
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'is_admin',
|
|
|
|
'gruppo',
|
2019-07-18 18:33:56 +02:00
|
|
|
'id_anagrafica',
|
2018-08-10 17:14:09 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'name', 'email', 'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $is_admin;
|
|
|
|
protected $gruppo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'password', 'remember_token',
|
|
|
|
];
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
/**
|
|
|
|
* Crea un nuovo utente.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $email
|
|
|
|
* @param string $password
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function build(Group $gruppo, $username, $email, $password)
|
|
|
|
{
|
2020-09-22 20:28:37 +02:00
|
|
|
$model = new static();
|
2019-07-18 18:33:56 +02:00
|
|
|
|
|
|
|
$model->group()->associate($gruppo);
|
|
|
|
|
|
|
|
$model->username = $username;
|
|
|
|
$model->email = $email;
|
|
|
|
$model->password = $password;
|
|
|
|
|
2019-07-26 11:57:59 +02:00
|
|
|
$model->enabled = 1;
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:14:09 +02:00
|
|
|
public function getIsAdminAttribute()
|
|
|
|
{
|
|
|
|
if (!isset($this->is_admin)) {
|
|
|
|
$this->is_admin = $this->getGruppoAttribute() == 'Amministratori';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->is_admin;
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
public function getIdAnagraficaAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes['idanagrafica'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setIdAnagraficaAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['idanagrafica'] = $value;
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:14:09 +02:00
|
|
|
public function getGruppoAttribute()
|
|
|
|
{
|
|
|
|
if (!isset($this->gruppo)) {
|
|
|
|
$this->gruppo = $this->group->nome;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->gruppo;
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
public function getSediAttribute()
|
|
|
|
{
|
|
|
|
$database = database();
|
|
|
|
|
|
|
|
// Estraggo le sedi dell'utente loggato
|
|
|
|
$sedi = $database->fetchArray('SELECT idsede FROM zz_user_sedi WHERE id_user='.prepare($this->id));
|
|
|
|
|
|
|
|
// Se l'utente non ha sedi, è come se ce le avesse tutte disponibili per retrocompatibilità
|
|
|
|
if (empty($sedi)) {
|
|
|
|
$sedi = $database->fetchArray('SELECT "0" AS idsede UNION SELECT id AS idsede FROM an_sedi WHERE idanagrafica='.prepare($this->idanagrafica));
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_column($sedi, 'idsede');
|
|
|
|
}
|
|
|
|
|
2019-07-16 15:43:15 +02:00
|
|
|
public function setPasswordAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['password'] = \Auth::hashPassword($value);
|
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
public function getPhotoAttribute()
|
|
|
|
{
|
|
|
|
if (empty($this->image_file_id)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image = Upload::find($this->image_file_id);
|
|
|
|
|
2020-09-23 17:53:19 +02:00
|
|
|
return base_path().'/'.$image->filepath;
|
2019-07-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setPhotoAttribute($value)
|
|
|
|
{
|
|
|
|
$module = \Modules::get('Utenti e permessi');
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'id_module' => $module->id,
|
|
|
|
'id_record' => $this->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Foto precedenti
|
|
|
|
$old_photo = Upload::where($data)->get();
|
|
|
|
|
|
|
|
// Informazioni sull'immagine
|
|
|
|
$filepath = is_array($value) ? $value['tmp_name'] : $value;
|
|
|
|
$info = Upload::getInfo(is_array($value) ? $value['name'] : $value);
|
2020-09-23 13:36:37 +02:00
|
|
|
$file = base_dir().'/files/temp_photo.'.$info['extension'];
|
2019-07-18 18:33:56 +02:00
|
|
|
|
|
|
|
// Ridimensionamento
|
|
|
|
$driver = extension_loaded('gd') ? 'gd' : 'imagick';
|
|
|
|
ImageManagerStatic::configure(['driver' => $driver]);
|
|
|
|
|
|
|
|
$img = ImageManagerStatic::make($filepath)->resize(100, 100, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
});
|
|
|
|
$img->save(slashes($file));
|
|
|
|
|
|
|
|
// Aggiunta nuova foto
|
|
|
|
$upload = Upload::build($file, $data);
|
|
|
|
|
|
|
|
// Rimozione foto precedenti
|
|
|
|
delete($file);
|
|
|
|
if (!empty($upload)) {
|
|
|
|
foreach ($old_photo as $old) {
|
|
|
|
$old->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->image_file_id = $upload->id;
|
|
|
|
}
|
|
|
|
|
2019-07-29 17:42:24 +02:00
|
|
|
public function getNomeCompletoAttribute()
|
|
|
|
{
|
|
|
|
$anagrafica = $this->anagrafica;
|
|
|
|
if (empty($anagrafica)) {
|
|
|
|
return $this->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $anagrafica->ragione_sociale.' ('.$this->username.')';
|
|
|
|
}
|
|
|
|
|
2020-02-24 12:21:47 +01:00
|
|
|
public function getApiTokens()
|
|
|
|
{
|
2020-02-23 14:21:49 +01:00
|
|
|
$query = 'SELECT * FROM `zz_tokens` WHERE `enabled` = 1 AND `id_utente` = '.prepare($this->id);
|
|
|
|
$database = database();
|
|
|
|
|
|
|
|
// Generazione del token per l'utente
|
|
|
|
$tokens = $database->fetchArray($query);
|
|
|
|
if (empty($tokens)) {
|
|
|
|
$token = secure_random_string();
|
|
|
|
|
|
|
|
$database->insert('zz_tokens', [
|
|
|
|
'id_utente' => $this->id,
|
|
|
|
'token' => $token,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $database->fetchArray($query);
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:14:09 +02:00
|
|
|
/* Relazioni Eloquent */
|
|
|
|
|
|
|
|
public function group()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Group::class, 'idgruppo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function logs()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Log::class, 'id_utente');
|
|
|
|
}
|
|
|
|
|
2019-07-26 11:57:59 +02:00
|
|
|
public function notes()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Note::class, 'id_utente');
|
|
|
|
}
|
|
|
|
|
2019-07-19 15:23:00 +02:00
|
|
|
public function anagrafica()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function image()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Upload::class, 'image_file_id');
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:14:09 +02:00
|
|
|
public function modules()
|
|
|
|
{
|
|
|
|
return $this->group->modules();
|
|
|
|
}
|
|
|
|
}
|