UserService - Quick Example
In this example, I want to display the login page if the user is not logged or a message if he's logged, managed by my userServiceController.php
First : Declare UserService on your controller
/** * @var UserService */ private $userSercice;
Then : Bind the UserService into userServiceController
Find userServiceController in Mouf interface and drag and drop the userService
instance to your UserService's variable:
Finally : Code the function which displays information
At this step, you can use the userservice-splash package to use the @Logged
annotation
userServiceController
:
/** * @var UserService */ private $userService; /** * @var UserBean */ protected $currentUser; /** * This page can be accessed only when a user is logged. * @URL protected_resource */ public function userServiceView() { $islogged = $this->userService->isLogged(); if(!$islogged){ $this->userService->redirectNotLogged(); return; } $this->currentUser = $this->userService->getLoggedUser(); $this->content->addFile(ROOT_PATH.'src/views/userService/myProtectedResource.php', $this); $this->template->toHtml(); }
My login view UserServiceLoginPage.php
<h1>LOGGIN PAGE</h1>
<form role="form" method="POST" action="login">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="login" name="login" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
My logged user view myProtectedResource.php
<h1>My Protected Resources</h1>
This page is accessible only if user is logged.
<br />
Actually, <b><?php echo $this->currentUser->getName()?></b> is logged.
<br />
<button type="button" onclick="logout()">Logout</button>
<script>
function logout(){
$.ajax({
url: rootUrl + "ajax/logout",
sucess : function(){
}
});
window.location.reload();
}
</script>
See this example thanks to MoufSample project. Now, just go to [my application]/protected_resource and test it !!!
See RightService documentation to manage users' rights
Found a typo? Something is wrong in this documentation? Just fork and edit it!