velsym/auth
Ready to use package for authentication in Velsym framework.
composer require velsym/auth
Velsym\Auth
Roles
A Singleton class that is used to define user roles that work inside the velsym application.
Methods
set(roles)
- defines roles working in the application.Returns:
void
Params:
roles
- key-value array of roles. Key is name of the role and value is an integer (can be used to defined hierarchy).
get()
- returns the list of roles.Returns:
array
Examples
use Velsym\Auth\Roles;
$roles = [
"USER" => 10,
"PREMIUM" => 20,
"ADMIN" => 30
];
Roles::set($roles);
BaseUser
An abstract class extended by BaseModel[Link Required]. Used to create custom user.
Properties
role
- takes one of the roles defined in the Roles.
Methods
getRole()
- returns the role that is assigned to the user.Returns:
string
setRole(role)
- used to promote/demote/change user's role.Returns:
void
Params:
role
- string of user's role.
Examples
use Velsym\Auth\BaseUser;
class BasicUser extends BaseUser
{
const TABLE_NAME = "basic_user";
private string $name;
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
AuthStatus
Manages status of the (un)authenticated user.
Methods
logIn(user)
- binds client with the user.Returns:
void
Params:
user
- Instance of class extended by BaseUser.
logOut()
- unbinds the user from client.Returns:
void
getUser()
- if user is logged in then the user object is returned. Otherwise null is returned.Returns:
NULL or instance of class extended by
BaseUser
isLoggedIn()
- checks if any user is bound to the client.Returns:
bool
- true if user is authenticated and false if not.
Examples
Example below uses BasicUser
from the example titled "How to create custom user" and role setup from the example title "Example role setup".
use Velsym\Auth\AuthStatus;
$user = new BasicUser();
$user->setRole("ADMIN");
$user->setName("Jordan");
AuthStatus::getUser(); // returns NULL.
AuthStatus::isLoggedIn(); // Returns false.
AuthStatus::logOut(); // Does nothing since user isn't logged it yet.
AuthStatus::logIn($user); // User is now logged in.
AuthStatus::getUser(); // returns copy of $user. NOT THE SAME INSTANCE!
// meaning that (===) will return false.
AuthStatus::isLoggedIn(); // Returns true.
AuthStatus::logOut(); // Logs out the user.
Velsym\Auth\Middleware
AuthCanAccess
Check if the user's role is sufficient to access route. If role has higher interger/value than required, then it will also be allowed.
Params
role
- role required to access the route.redirectPath
- url to which client should be redirected.
Examples
use Velsym\Routing\RouteHelper;
use Velsym\Routing\Attributes\Route;
use Velsym\Routing\Attributes\Middleware;
use Velsym\Communication\Response;
use Velsym\Auth\Middleware\AuthCanAccess;
class ExampleRoutes extends RouteHelper
{
#[Middleware(
middleware: AuthCanAccess::class,
params: [ 'role' => "ADMIN", 'redirectPath' => "/home" ]
)]
#[Route( path: "/admin", methods: ["GET", "POST"] )]
public function home(): Response
{
// ...
}
}
Last updated