<commit_before><?php namespace Gzero\Entity;

use Laravel\Passport\HasApiTokens;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Gzero\Entity\Presenter\UserPresenter;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Robbo\Presenter\PresentableInterface;

/**
 * This file is part of the GZERO CMS package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Class User
 *
 * @package    Gzero\Entity
 * @author     <NAME> <<EMAIL>>
 * @copyright  Copyright (c) 2014, <NAME>
 */
class User extends Base implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, PresentableInterface {

    use Authenticatable, Authorizable, CanResetPassword, HasApiTokens;

    /**@TODO proper method for adding new fillable fields from package with migrations */
    /**
     * @var array
     */
    protected $fillable = [
        'email',
        'first_name',
        'has_social_integrations',
        'last_name',
        'nick',
        'password',
        'remember_token'
    ];

    /**
     * @var array
     */
    protected $attributes = [
        'is_admin' => false
    ];

    /**
     * Permission map
     *
     * @var array
     */
    protected $permissionsMap = null;

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password'];

    /**
     * The roles that belong to the user.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'acl_user_role')->withTimestamps();
    }

    /**
     * Checks is user have super admin permissions
     *
     * @return boolean
     */
    public function isSuperAdmin()
    {
        return (boolean) $this->is_admin;
    }

    /**
     * It checks if given user have specified permission
     *
     * @param string $permission Permission name
     *
     * @return bool
     */
    public function hasPermission($permission)
    {
        if (!is_array($this->permissionsMap)) {
            $permissionsMap = cache()->get('permissions:' . $this->id, null);
            if ($permissionsMap === null) { // Not in cache
                $this->permissionsMap = $this->buildPermissionsMap();
                cache()->forever('permissions:' . $this->id, $this->permissionsMap);
            } else {
                $this->permissionsMap = $permissionsMap;
            }
        }
        return in_array($permission, $this->permissionsMap);
    }

    /**
     * Return a created presenter.
     *
     * @return \Robbo\Presenter\Presenter<commit_msg>Added isGuest method to user<commit_after><?php namespace Gzero\Entity;

use Laravel\Passport\HasApiTokens;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Gzero\Entity\Presenter\UserPresenter;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Robbo\Presenter\PresentableInterface;

/**
 * This file is part of the GZERO CMS package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Class User
 *
 * @package    Gzero\Entity
 * @author     <NAME> <<EMAIL>>
 * @copyright  Copyright (c) 2014, <NAME>
 */
class User extends Base implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, PresentableInterface {

    use Authenticatable, Authorizable, CanResetPassword, HasApiTokens;

    /**@TODO proper method for adding new fillable fields from package with migrations */
    /**
     * @var array
     */
    protected $fillable = [
        'email',
        'first_name',
        'has_social_integrations',
        'last_name',
        'nick',
        'password',
        'remember_token'
    ];

    /**
     * @var array
     */
    protected $attributes = [
        'is_admin' => false
    ];

    /**
     * Permission map
     *
     * @var array
     */
    protected $permissionsMap = null;

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password'];

    /**
     * The roles that belong to the user.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'acl_user_role')->withTimestamps();
    }

    /**
     * Checks is user have super admin permissions
     *
     * @return boolean
     */
    public function isSuperAdmin()
    {
        return (boolean) $this->is_admin;
    }

    /**
     * Only GuestUser should have it set to true
     *
     * @return boolean
     */
    public function isGuest()
    {
        return false;
    }

    /**
     * It checks if given user have specified permission
     *
     * @param string $permission Permission name
     *
     * @return bool
     */
    public function hasPermission($permission)
    {
        if (!is_array($this->permissionsMap)) {
            $permissionsMap = cache()->get('permissions:' . $this->id, null);
            if ($permissionsMap === null) { // Not in cache
                $this->permissionsMap = $this->buildPermissionsMap();
                cache()->forever('permissions:' . $this->id, $this->permissionsMap);
            } else {
                $this->permissionsMap = $permissionsMap;
            }
        }
        return in_array($permission, $this->permissionsMap);
    }

    /**
     * Return a created presenter.
     *
     * @return \Robbo\Presenter\Presenter
{'fraction_non_alphanumeric': 0.11036515474046177, 'fraction_numerical': 0.0013099721630915344, 'mean_word_length': 2.494279176201373, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3069401', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1706, 'n_words': 432}