TODO

@param $instance Controller
@param $user User
@return bool
334328
public function isAllowed($instance, $user)
    {
        if ($user) {
            if (!is_string($instance)) {
                if ($instance->admin && $user->admin === false) {
                    return false;
                }
                // El admin tiene acceso a cualquier pagina
                if ($user->admin === 1) {
                    return true;
                }
            }
            $page = new Page();
            $current = $page->get_by_controller($instance);
            if ($current && $current->count() > 0) {
                $result = $this->getByPageUser($user->id, $current->getModel()->id);
                if ($result->count() > 0) {
                    return $result->getModel()->allowed;
                }
            } else {
                return false;
            }
        }

        return true;
    }
241637
public function verifyEmail(User $user, $token)
    {
        $this->_prepareEmail($user, __d('wasabi_core', 'Verify your email address'));
        $this->_email->template('Wasabi/Core.User/verify');
        $this->_email->viewVars([
            'user' => $user,
            'verifyEmailLink' => [
                'plugin' => 'Wasabi/Core',
                'controller' => 'Users',
                'action' => 'verifyByToken',
                'token' => $token
            ],
            'instanceName' => Wasabi::getInstanceName()
        ]);
    }
417575
public function unenrol_user(stdClass $instance, $userid) {
        global $DB;

        // Get the tool associated with this instance. Note - it may not exist if we have deleted
        // the tool. This is fine because we have already cleaned the 'enrol_lti_users' table.
        if ($tool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), 'id')) {
            // Need to remove the user from the users table.
            $DB->delete_records('enrol_lti_users', array('userid' => $userid, 'toolid' => $tool->id));
        }

        parent::unenrol_user($instance, $userid);
    }
698481
public function saveUser($user) {
        global $DB;

        $now = time();
        $isinsert = is_null($user->created);
        $user->updated = $now;

        $params = [
            'ltiresultsourcedid' => $user->ltiResultSourcedId,
            'updated' => $user->updated
        ];

        if ($isinsert) {
            $params['resourcelinkid'] = $user->getResourceLink()->getRecordId();
            $params['ltiuserkey'] = $user->getId(ToolProvider\ToolProvider::ID_SCOPE_ID_ONLY);
            $user->created = $now;
            $params['created'] = $user->created;
            $id = $DB->insert_record($this->userresulttable, (object) $params);
            if ($id) {
                $user->setRecordId($id);
                return true;
            }

        } else {
            $params['id'] = $user->getRecordId();
            return $DB->update_record($this->userresulttable, (object) $params);
        }

        return false;
    }
422927
public static function instance($instance = null)
	{
		if ($instance !== null)
		{
			if ( ! array_key_exists($instance, static::$_instances))
			{
				return false;
			}

			return static::$_instances[$instance];
		}

		if (static::$_instance === null)
		{
			static::$_instance = static::forge();
		}

		return static::$_instance;
	}
1338954
public function verify( $user, $token ) {

		// Check Token
		if( $user->isVerifyTokenValid( $token ) ) {

			$modelClass	= static::$modelClass;

			// Find existing user
			$userToUpdate = $modelClass::findById( $user->id );

			// User need admin approval
			if( Yii::$app->core->isUserApproval() ) {

				$userToUpdate->verify();
			}
			// Direct approval and activation
			else {

				$userToUpdate->status = User::STATUS_ACTIVE;
			}

			$userToUpdate->unsetVerifyToken();

			$userToUpdate->update();

			return true;
		}

		return false;
	}
586441
public function save(IUser $user): bool
    {
        if ($user instanceof AnonymousUser) {
            return false;
        }

        if ($user->isNew()) {
            $roles = $this->filterRoles($user->getUserRoles());
            $user->addRoles(array_values($roles));
            return entityManager()->save($user);
        }

        if (!entityManager()->save($user)) {
            return false;
        }

        if (null !== $roles = $user->getUserRoles()) {
            $toAdd = $this->filterRoles($roles);
            $toRemove = [];

            foreach ($user->roles() as $role) {
                $name = $role->name();
                if (!isset($toAdd[$name])) {
                    $toRemove[] = $role;
                } else {
                    unset($toAdd[$name]);
                }
            }

            if (!empty($toRemove)) {
                $user->removeRoles($toRemove);
            }

            if (!empty($toAdd)) {
                $user->addRoles(array_values($toAdd));
            }
        }

        return true;
    }
30859
public static function instance($user, ?\context $context = null) {

        $userid = $user;
        if (!is_scalar($userid)) {
            $userid = $user->id;
        }

        if (self::$cachedid === $userid) {
            return self::$cachedinstance;
        }

        $cachedinstance = new \core_analytics\user($user, $context);
        self::$cachedinstance = $cachedinstance;
        self::$cachedid = (int)$userid;
        return self::$cachedinstance;
    }
298712
public function isSatisfiedBy(User $user)
    {
        return $this->email === $user->email && password_verify($this->password, $user->password);
    }
1019649
public function deleteUser(User $user)
    {
        $parent = $user->getParent();
        if (!isset($this->userList[$user->getUsername()]) || $this->userList[$user->getUsername()] !== $user ||
            $parent == null
        ) {
            return false;
        }
        $this->connection->beginTransaction();
        $children = $this->getChildren($user);
        $success = true;
        foreach ($children as $child) {
            /** @var $child User */
            $success = $success && $child->setParent($parent);
        }
        $success = $success && $user->delete();

        if ($success) {
            $this->connection->commit();
            $this->setUpIterator();
        } else {
            $this->connection->rollBack();
        }
        return $success;
    }
604188
public function isSuperiorThen($user)
    {
        if( ! $this->maxRole()) return false;
        if( ! $user->maxRole()) return true;
        return $this->maxRole()->order > $user->maxRole()->order;
    }
193132
public function getUserMainDomain(User $user) {
    if($this->domainEnabled) {
      $groups = Group::getUserGroup($user);
      if (is_array($groups)) {
        foreach ($groups as $group) {
          if ($domaine = $group->getDomain()) {
            if ($domaine->status()) {
              return $domaine;
            }
          }
        }
      }
    }
    return NULL;
  }
762206
public function hasAccess(array $url, array $user): bool
    {
        if (!empty($url['action']) && !empty($url['controller'])) {
            $controllerName = Utils::normalizeControllerName($url);
            if ($this->isSkipAction($controllerName, $url['action'])) {
                return true;
            }
        }

        if (!empty($url['controller']) && $this->isSkipController($url['controller'])) {
            return true;
        }

        if (!empty($url['action']) && $this->isSkipControllerActions($url['controller'], $url['action'])) {
            return true;
        }

        return false;
    }
527988
public function render_instance( $instance_id, $instance ) {
		$this->instance = $instance_id;
		$has_data       = array_filter( $instance );
		if ( ! empty( $has_data ) ) {
			if ( ! isset( $this->struct['active'] ) ) {
				$this->struct['active'] = 'true';
			}

			return $this->render_repeatable();
		}

		return '';
	}
1173382
public function persistUser(User $user)
    {
        $this->objectManager->persist($user);
        $this->objectManager->flush();

        return $user;
    }
602755
public function completed(UserInterface $user)
    {
        $queryBuilder = $this->createQueryBuilder('a');

        $queryBuilder
            ->where('a.user = :user')
            ->andWhere('a.completed = :completed');

        $queryBuilder
            ->setParameters([
                'user' => $user,
                'completed' => true
            ]);

        try
        {
            return $queryBuilder->getQuery()->getSingleResult();
        }
        catch (NoResultException $e)
        {
            return false;
        }
    }
831889
public static function &getInstance( $instance = null )
	{
		if ( is_null( self::$instance ) )
		{
			self::$instance = $instance ? $instance : new self();
			// if ( $callback ) $callback( self::$instance );
		}
		return self::$instance;
	}