%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/User.php

<?php

namespace App;

use App\Enums\UsertypeEnum;
use App\Models\Communication;
use App\Models\Individual;
use App\Models\Institute;
use App\Models\Minor;
use App\Models\Usertype;
use App\Scopes\IndividualScope;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Nexmo\Response;
use Spatie\Permission\Traits\HasRoles;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Watson\Validating\ValidatingTrait;

class User extends Authenticatable
{
    use Notifiable, HasRoles, HasApiTokens, ValidatingTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'usertype_id',
        'remember_token',
        'active',
        'pusher_id'
    ];

    protected $attributes = [
        'active' => false,
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $rules = [
        'name'        => 'required|string',
        'email'       => 'required|string|unique:users,email',
        'usertype_id' => 'required|integer|exists:usertypes,id',
        'active'      => 'boolean'
    ];

    /**
     * Validation messages to be passed to the validator.
     *
     * @var array
     */
    protected $validationMessages = [
        'name.required'     => "Il nome è obbligatorio",
        'email.required'    => "Lo username è obbligatorio",
        'email.unique'      => "Questo username è già presente",
        'password.required' => 'La password è obbligatioria',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function usertype()
    {
        return $this->belongsTo(Usertype::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function individual()
    {
        return $this->hasOne(Individual::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function institutes()
    {
        return $this->belongsToMany(Institute::class);
    }

    public function communicationsSent() {
        return $this->hasMany(Communication::class, 'sender_id', 'id');
    }

    public function communicationsReceived() {
        return $this->hasMany(Communication::class, 'receiver_id', 'id');
    }
    /**
     * @param Minor $minor
     * @param bool $httpException
     * @return bool
     */
    public function isTeacher(Minor $minor, $httpException = true)
    {
        if ($this->isAdmin()) return true;
        if ($this->role() != "teacher") return false;

        $teacher_institutes = $this->institutes->map(function ($item, $key){
            return $item->id;
        });

        $is_teacher = in_array($minor->institute->id, $teacher_institutes->toArray());

        if ($httpException && !$is_teacher) {
            throw new HttpException(\Illuminate\Http\Response::HTTP_UNAUTHORIZED, __('user_not_allowed'));
        }

        return $is_teacher;
    }

    /**
     * @param Minor $minor
     * @param bool $httpException
     * @return int
     * @internal param bool $throwException
     */
    public function isTutor(Minor $minor, $httpException = true)
    {
        if ($this->isAdmin()) return true;
        if ($this->role() != "tutor") return false;

        $is_tutor = $minor->individuals()
            ->where('individual_id', $this->individual->id)
            ->count();

        if ($httpException && !$is_tutor) {
            throw new HttpException(\Illuminate\Http\Response::HTTP_FORBIDDEN, __('user_not_allowed'));
        }

        return $is_tutor;
    }

    public function isFamilyAdmin(Minor $minor, $httpException = true)
    {
        if ($this->isAdmin()) return true;

        $is_family_admin = $minor->individuals()
            ->where('individual_id', $this->individual->id)
            ->where('is_admin', '=', true)
            ->count();

        if ($httpException && !$is_family_admin) {
            throw new HttpException(\Illuminate\Http\Response::HTTP_UNAUTHORIZED, __('user_not_allowed'));
        }

        return $is_family_admin;
    }

    /**
     * @return bool
     */
    public function isAdmin()
    {
        return $this->usertype->role === UsertypeEnum::ADMIN;
    }

    /**
     * Checks weather the user is principal of a given institute
     * @param Institute $institute
     * @return int
     */
    public function isPrincipalOf(Institute $institute)
    {
        return $this->institutes()->where('institute_id', $institute->id)->count();
    }

    /**
     * @return mixed
     */
    public function role()
    {
        return  $this->usertype->role;
    }

    public function getEmailForPasswordReset()
    {
        return $this->individual()->withoutGlobalScope(IndividualScope::class)->first()->email;
    }

    public function routeNotificationForMail()
    {
        return $this->getEmailForPasswordReset();
    }

}

Zerion Mini Shell 1.0