webCOMAND

io_comand_login\models\user::UserModel

The default User Login Model that implements the user interface for the built-in webCOMAND User content type or a content type that extends it.  Extend and override or replaced to support a custom user content type.

It implements the following methods, which are required by the user interface.

  • public function get_user_by_id( $id )
  • public function get_user_by_username( string $username )
  • public function get_user_by_email( string $email )
  • public function get_user_id( $user ): int
  • public function get_username( $user ): string
  • public function get_name( $user ): string
  • public function is_active_user( $user ): bool

Example Alternative

To use the login API with a custom user content type that does not extend the built-in webCOMAND User content type, you need to implement an alternative to allow the login API to interface with your user content.  Here is an example.

For example, if your new content type has the Identifier "MyUser", create a "PHP File" in your package with Filename "models/MyModel.php" and the following code.

<?php
namespace com_example_www\models;

class MyUser extends \io_comand_login\models\user\UserModel {
    const IDENTIFIER = 'MyUser';

    public function get_user_by_id($id) {
        $repo = $this->login->get('repo') || $this->repo();

        return $repo->query()
            ->from(self::IDENTIFIER . '+')
            ->and_where( '=', 'OID', $id)
            ->get_first();
    }

    public function get_user_by_username( string $username ) {
        $repo = $this->login->get('repo') || $this->repo();

        return $repo->query()
            ->from(self::IDENTIFIER . '+')
            ->and_where('=', ['LOWER', 'Username'], strtolower($username))
            ->get_first();
    }

    public function get_user_by_email(string $email) {
        $repo = $this->login->get('repo') || $this->repo();

        return $repo->query()
            ->from(self::IDENTIFIER . '+')
            ->and_where('=', "Email", strtolower($email))
            ->get_first();
    }

    public function get_user_id($user): int {
        if( !($user instanceof \comand\models\Object) || !$user->is_instanceof(self::IDENTIFIER) )
            throw new \comand\exception("Unrecognized user object given - ".get_class($user));
        return $user->OID;
    }

    public function get_username($user): string {
        if( !($user instanceof \comand\models\Object) || !$user->is_instanceof(self::IDENTIFIER) )
            throw new \comand\exception("Unrecognized user object given - ".get_class($user));
        return $user->Username;
    }

    public function get_name($user): string {
        if( !($user instanceof \comand\models\Object) || !$user->is_instanceof(self::IDENTIFIER) )
            throw new \comand\exception("Unrecognized user object given - ".get_class($user));
        return $user->get_full_name();
    }

    public function get_email($user): string {
        if( !($user instanceof \comand\models\Object) || !$user->is_instanceof(self::IDENTIFIER) )
            throw new \comand\exception("Unrecognized user object given - ".get_class($user));
        return $user->Email;
    }

    public function is_active_user($user): bool {
        if( !($user instanceof \comand\models\Object) || !$user->is_instanceof(self::IDENTIFIER) )
            throw new \comand\exception("Unrecognized user object given - ".get_class($user));
        return $user->Active == 1;
    }

    public function is_administrator($user): bool {
        if( !($user instanceof \comand\models\Object) || !$user->is_instanceof(self::IDENTIFIER) )
            throw new \comand\exception("Unrecognized user object given - ".get_class($user));
        return $user->is_super_user();
    }
}