Authentication

Overview #

In this section, we will learn how to provide user information into Dashboard application. There are two cases. The first case is when you had application with user authentication available, you can provide those information to dashboard. Second case is when you do not have login system and would like user to login in order to access dashboard application.

User #

Dashboard provide you with a User class which you can create and inject user information into Dashboard. Dashboard will use this user information to authorize access as well as to display user information.

Provide user information to dashboard application

use \koolreport\dashboard\User;

App::create()
->user(
    User::create()
    ->id(1)
    ->name("John Doe")
    ->avatar("/assets/joe/image.jpg")
    ->roles(["staff","manager"])
    ->others([
        "anyname"=>"anyvalue"
    ])
)
->run();

Inside the application, you can retrieve information

class App extends Application
{
    protected function sidebar()
    {
        return [
            //ManagerBoard is shown only for manager
            "ManagerBoard">ManagerBoard::create()->enabled(function($request) {
                return $request->user()->hasRole("manager");
            }),
            ...
        ];
    }
}

Properties #

Nametypedescription
idint/stringSet unique id for user
namestringSet name for user
avatarstringSet path to avatar image
rolesarraySet list of roles of user for example ['staff','manager']
othersarraySet extended user information in form of array

Note: Those above properties follows this code rules.

Methods #

Nametypedescription
hasRole(string $roleName)booleanCheck whether user has a specific role

Login #

Consider the case when content of dashboard is protected and require authentication in order to see dashboard content. The user will go to dashboard, provide login information. If everything is great, he will be able to access dashboard content.

Dashboard Framework provide you an built-in login page which will provide authentication when turn on. To enable this feature, you need to overwrite Application login() function and return the \koolreport\dasboard\pages\Login object:

use \koolreport\dashboard\pages\Login;
use \koolreport\dashboard\User;

class App extends Application
{
    protected function login()
    {
        return Login::create()
            ->headerText("Login")
            ->descriptionText("Sign in to your account")
            ->buttonText("Login")
            ->failedText("Wrong username or password")
            ->rightImageUrl("/assets/login/image.jpg")
            ->authenticate(function($username,$password){
                //You can make database connection in here to authenticate $username or password
                //Return User class when it is authenticated

                if($username==="johndoe" && $password==="johndoe") {
                    return User::create()
                        ->name("John Doe")
                        ->id(1)
                        ->roles(["guest"]);
                }
                return null;
            });
    }
}

Properties #

Nametypedescription
headerTextstringSet the header text on login form
descriptionTextstringSet the description text of login form
failedTextstringSet the text shown when failed to authenticate
rightImageUrlstringSet the image url of image on the right of login form, just for beauty of page

Note: Those above properties follows this code rules.

Methods #

Nametypedescription
authenticate(function $handler)Set the authentication function, the function will received $username and $password as parameters, you will need to decide whether this information is correct and provide User information in case provided information is correct

Get started with KoolReport

KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.