Hello! Here's a little tutorial for how to use:
...to restrict access to admins. Currently you can use both:
...to restrict access to members and guests respectively (leave the brackets blank if anyone can access). But what if you want to create a page that only admins can see? Perhaps you want to create a special reward page for your admins, or you want to work on a feature without risking members having access to it before it's ready.
First up, where does that line go? If you've tinkered with the mysidia files, you've probably seen that it goes in your controller file. For this example I'm going to be creating a page called 'test' btw.
1) Now, the setup. Open '...resource/core/appcontroller.php'.
At the top, among the 'use Resource' lines, put:
I put it underneath the line that adds GuestNoaccessException.
We need this line because we're going to create a new error for members trying to access the page.
2) Next, scroll down to this section, around line 143/144:
Replace it with:
You can see where I added a new section for "admin" and used the new error we're going to make.
3) Next, open '...resource/core/mysidia.php'. Scroll down to the 'displayError' function around line 380 and replace it with this:
You can see that I added a new case called "admin" that will display an error title and error message if users are not admins.
4) Next, open '...lang/lang_global.php' and add these two lines:
Feel free to customise them a little if you want the error to say something different!
5) In the folder '...resource/exception' create a new file called 'membernoaccessexception.php'. Put this inside:
6) Finally we get to create our pages!
Create a new view file in '...view/main'. Call it testview.php.
Put this inside:
And in '...controller/main' create a new file called testcontroller.php and put this inside:
And that's it! Now you can restrict entire pages to admins if you want to. Personally I'll be using this when creating new features that I want to test before letting regular members use it...
Feel free to create your own uses for this tutorial as well! Want to restrict a page to 'premium' members? Create a new case for "premium". Want to only allow certain usergroups? You can totally do that. If you need any help I can try to troubleshoot.
I attached the files as a .zip file. If you drag them into your project it will ask to override. If you haven't touched appcontroller.php, mysidia.php, or lang_global.php, then it should be safe.
PHP:
parent::__construct();
...to restrict access to admins. Currently you can use both:
PHP:
parent::__construct("member");
parent::__construct("guest");
...to restrict access to members and guests respectively (leave the brackets blank if anyone can access). But what if you want to create a page that only admins can see? Perhaps you want to create a special reward page for your admins, or you want to work on a feature without risking members having access to it before it's ready.
First up, where does that line go? If you've tinkered with the mysidia files, you've probably seen that it goes in your controller file. For this example I'm going to be creating a page called 'test' btw.
1) Now, the setup. Open '...resource/core/appcontroller.php'.
At the top, among the 'use Resource' lines, put:
PHP:
use Resource\Exception\MemberNoaccessException;
I put it underneath the line that adds GuestNoaccessException.
We need this line because we're going to create a new error for members trying to access the page.
2) Next, scroll down to this section, around line 143/144:
PHP:
protected function handleAccess(){
$mysidia = Registry::get("mysidia");
if($this->access == "member" && !$mysidia->user->isLoggedIn()){
throw new GuestNoaccessException($mysidia->lang->global_guest);
}
if($this->access == "guest" && $mysidia->user->isLoggedIn()){
throw new AlreadyLoggedinException($mysidia->lang->global_login);
}
}
Replace it with:
PHP:
protected function handleAccess(){
$mysidia = Registry::get("mysidia");
if($this->access == "admin" && !$mysidia->user->isAdmin()){
throw new MemberNoaccessException($mysidia->lang->global_member);
}
if($this->access == "member" && !$mysidia->user->isLoggedIn()){
throw new GuestNoaccessException($mysidia->lang->global_guest);
}
if($this->access == "guest" && $mysidia->user->isLoggedIn()){
throw new AlreadyLoggedinException($mysidia->lang->global_login);
}
}
You can see where I added a new section for "admin" and used the new error we're going to make.
3) Next, open '...resource/core/mysidia.php'. Scroll down to the 'displayError' function around line 380 and replace it with this:
PHP:
public function displayError($error){
$document = $this->frame->getDocument();
switch($error){
case "register":
$document->setTitle($this->lang->global_register_title);
$document->addLangvar($this->lang->global_register);
break;
case "login":
$document->setTitle($this->lang->global_login_title);
$document->addLangvar($this->lang->global_login);
break;
case "guest":
$document->setTitle($this->lang->global_guest_title);
$document->addLangvar($this->lang->global_guest);
break;
case "admin":
$document->setTitle($this->lang->global_member_title);
$document->addLangvar($this->lang->global_member);
break;
case "id":
$document->setTitle($this->lang->global_id_title);
$document->addLangvar($this->lang->global_id);
break;
case "action":
$document->setTitle($this->lang->global_action_title);
$document->addLangvar($this->lang->global_action);
break;
case "session":
$document->setTitle($this->lang->global_session_title);
$document->addLangvar($this->lang->global_session);
break;
case "access":
$document->setTitle($this->lang->global_access_title);
$document->addLangvar($this->lang->global_access);
break;
default:
$document->setTitle($this->lang->global_error);
$document->addLangvar($this->lang->global_error);
}
}
You can see that I added a new case called "admin" that will display an error title and error message if users are not admins.
4) Next, open '...lang/lang_global.php' and add these two lines:
PHP:
$lang['global_member_title'] = "Access Denied";
$lang['global_member'] = "Only admins may access this page. If you feel this is an error, please contact an admin for support.";
Feel free to customise them a little if you want the error to say something different!
5) In the folder '...resource/exception' create a new file called 'membernoaccessexception.php'. Put this inside:
PHP:
<?php
namespace Resource\Exception;
use Exception;
class MemberNoaccessException extends Exception{
}
6) Finally we get to create our pages!
Create a new view file in '...view/main'. Call it testview.php.
Put this inside:
PHP:
<?php
namespace View\Main;
use Smarty;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\Core\Model;
use Resource\Model\DomainModel;
use Resource\GUI\Document\Comment;
class TestView extends View{
public function index(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$document->setTitle("Test");
$document->add(new Comment("Only admins can access this page, yay!"));
}
}
And in '...controller/main' create a new file called testcontroller.php and put this inside:
PHP:
<?php
namespace Controller\Main;
use Resource\Core\AppController;
use Resource\Core\Registry;
class TestController extends AppController{
public function __construct(){
parent::__construct("admin");
}
public function index(){
$mysidia = Registry::get("mysidia");
}
}
And that's it! Now you can restrict entire pages to admins if you want to. Personally I'll be using this when creating new features that I want to test before letting regular members use it...
Feel free to create your own uses for this tutorial as well! Want to restrict a page to 'premium' members? Create a new case for "premium". Want to only allow certain usergroups? You can totally do that. If you need any help I can try to troubleshoot.
I attached the files as a .zip file. If you drag them into your project it will ask to override. If you haven't touched appcontroller.php, mysidia.php, or lang_global.php, then it should be safe.