October CMS Static Pages plugin - hide / show pages in the backend based on user roles? - laravel

How can I hide some static pages based on the user's role?
I defined the role of users with the name "blabla".
Now I want to hide all the pages from these users, except for the page "blabla" in the "Static Pages" backend.
How can i do this?
sorry for my English))

yes of course you can do it but we need to write some code here.
we can utilize cms.object.listInTheme event
In your plugin within boot method you can add this event listener and filter static pages.
\Event::listen('cms.object.listInTheme', function ($cmsObject, $objectList) {
// lets check if we are really running in static pages
// you can also add more checks here based on controllers etc ..
if ($cmsObject instanceof \RainLab\Pages\Classes\Page) {
$user = \BackendAuth::getUser();
// role code and role name are different things
// we should use role code as it act as constant
$hasRoleFromWhichIneedTohidePages = $user->role->code === 'blabla' ? true : false;
// if user has that role then we start filtering
if($hasRoleFromWhichIneedTohidePages) {
foreach ($objectList as $index => $page) {
// we can use different matching you can use one of them
// to identify your page which you want to hide.
// forgot method will hide that page
// match against filename
if ($page->fileName == 'hidethispage.htm') {
$objectList->forget($index);
}
// OR match against title
if ($page->title == 'hidethispage') {
$objectList->forget($index);
}
// OR match against url
if ($page->url == '/hidethispage') {
$objectList->forget($index);
}
}
}
}
});
currently this code will check page-url / title / file-name and restrict user statically from showing page in list but you can put your own logic here and make things dynamic.
if you didn't get it or want dynamic solution then please comment , I will explain in more detail.

Related

How to use "OR" in route parameter?

I'm building a twitter-ish website and I'm having a problem with routing:
this code will bring the user to the profile page of the person with the given id.
Route::get('/profile/{id}', 'ProfileController#show')->name('profile.show');
this code will bring the user to the profile page of the person with the given username.
Route::get('/profile/{username}', 'ProfileController#show')->name('profile.show');
and finally, this code will bring the user to the profile page of the person with the given email.
Route::get('/profile/{email}', 'ProfileController#show')->name('profile.show');
I mean all these three URLs will show the user the same page:
example.com/profile/1
example.com/profile/rahimi0151
example.com/profile/rahimi0151#gmail.com
my question is:
is there a way to merge all these routes? like below:
Route::get('/profile/{id|username|email}', 'ProfileController#show')->name('profile.show');
Am not sure about merging the routes but you could write your route like this
Route::get('/profile/{identifier}', 'ProfileController#show')->name('profile.show');
and then change the method signature for show in ProfileController to something like this
public function show($identifier) {
if (is_numeric($identifier)) {
// do something
} else if ($this->isEmail($identifier)) {
// do something
} else {
// assume it is a username, and do something with that
}
}
// method to check if value provided is an email
// preferably, move this to a file of your custom helper functions
private function isEmail($value) {
// check if value is an email
// and return true/false indicating whether value is an email
}
And here is a link for a good way on how to check if a value is valid email address

How to render a cms page with default theme AND variables from controllers in OctoberCMS?

I'm wondering how I can render a view, or display a page with my default theme in OctoberCMS, via a route that executes a function in a controller.
If I have the following route:
Route::get('bransje', [
'uses' => 'Ekstremedia\Cityportal\CPController#bransje'
]);
And in my controller CPController ive tried several things, like I used to with Laravel:
public function bransje() {
$stuff = Stuff::with('info');
return View::make('cms::bransje')->with('stuff',$stuff);
}
But I cannot seem to get it to work, and I've tried to search the web, but it's hard to find answers. I have found a workaround, and that is to make a plugin component, then I can include that component and do:
public function onRun()
{
$this->eventen = $this->page['stuff'] = $this->stuff();
}
protected function stuff()
{
return ...
}
Is there any way so I can make pages without using the Cms, and that are wrapped in my default theme? I've tried
return View::make('my-theme-name::page');
and a lot of variants but no luck.
I know I can also do a:
==
public function onRun()
{
}
in the start of my page in the cms, but I'm not sure how to call a function from my plugin controller via there.
You can bypass frontend routing by using routes.php file in your plugin.
Full example in this video turotial.
If this answer can still be useful (Worked for October v434).
I have almost the same scenerio.
What I want to achieve is a type of routing like facebook page and profile.
facebook.com/myprofile is the same url structure as facebook.com/mypage
First I create a page in the CMS for each scenario (say catchpage.htm)
Then a created a catchall route at the buttom of routes.php in my plugin that will also not disturb the internal working of octobercms.
if (!Request::is('combine/*') && !Request::is('backend/*') && !Request::is('backend')) {
// Last fail over for looking up slug from the database
Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {
//Pretend this are our routes and we can check them against the database
$routes = ["bola", "sade", "bisi", "ade", "tayo"];
if(in_array($slug, $routes)) {
$cmsController = new Cms\Classes\Controller;
return $cmsController->render("/catchpage", ['slug' => $slug]);
}
// Some fallback to 404
return Response::make(View::make('cms::404'), 404);
});
}
The if Request::is check is a list of all the resource that october uses under the hood, please dont remove the combine as it is the combiner route. Remove it and the style and script will not render. Also the backend is the url to the backend, make sure to supply the backend and the backend/*.
Finally don't forget to return Response::make(View::make('cms::404'), 404); if the resource is useless.
You may put all these in a controller though.
If anyone has a better workaround, please let us know.

silverstripe static publisher - pages affected by DataObject Changes

is there a possibility to trigger an update of the cache if a DataObject is edited?
for example updating a News DataObject should update the cache of pages, that are displaying these NewsObjects.
many thanx,
Florian
Here is what I could do using the StaticPublishQueue module. In your NewsDataObject.php:
function onAfterWrite() {
parent::onAfterWrite();
$url = array();
$pages = $this->Pages(); //has_many link to pages that include this DataObject
foreach($pages as $page) {
$pagesAffected = $page->pagesAffected();
if ($pagesAffected && count($pagesAffected) > 0) {
$urls = array_merge((array)$urls, (array)$pagesAffected);
}
}
URLArrayObject::add_urls($urls);
}
This takes each of the pages that references your DataObject, asks it for all it's URL and the URL of any related pages (e.g. Virtual Pages that reference that page), compiles all the URLs into a big array, then adds that array to the static publishing queue. The queue will gradually process until all the affected pages are republished.
The event system allows you to add a layer of abstraction between the republishing and the triggers for republishing, but for something simple you don't necessarily need to use it. Instead, you can add pages to the queue directly. (You might also like to read this blog post describing the StaticPublishQueue module)
The StaticPublisherQueue module will handle that for you.
In case anyone else comes across this, and doesn't wish to use the StaticPublishQueue module instead of StaticPublisher, it does appear to be possible in StaticPublisher, the following works for me:
function onAfterWrite() {
parent::onAfterWrite();
$urls = array();
$pages = Page::get();
foreach($pages as $page) {
$urls[] = $page->Link();
}
$sp = new FilesystemPublisher();
$sp->publishPages($urls);
}
Note the last 2 lines, and use the Page::get to specify the exact pages that need to be updated.

Codeigniter - best routes configuration for CMS?

I would like to create a custom CMS within Codeigniter, and I need a mechanism to route general pages to a default controller - for instance:
mydomain.com/about
mydomain.com/services/maintenance
These would be routed through my pagehandler controller. The default routing behaviour in Codeigniter is of course to route to a matching controller and method, so with the above examples it would require an About controller and a Services controller. This is obviously not a practical or even sensible approach.
I've seen the following solution to place in routes.php:
$route['^(?!admin|products).*'] = "pagehandler/$0";
But this poses it's own problems I believe. For example, it simply looks for "products" in the request uri and if found routes to the Products controller - but what if we have services/products as a CMS page? Does this not then get routed to the products controller?
Is there a perfect approach to this? I don't wish to have a routing where all CMS content is prefixed with the controller name, but I also need to be able to generically override the routing for other controllers.
If you use CodeIgniter 2.0 (which has been stable enough to use for months) then you can use:
$route['404_override'] = 'pages';
This will send anything that isn't a controller, method or valid route to your pages controller. Then you can use whatever PHP you like to either show the page or show a much nicer 404 page.
Read me guide explaining how you upgrade to CodeIgniter 2.0. Also, you might be interested in using an existing CMS such as PyroCMS which is now nearing the final v1.0 and has a massive following.
You are in luck. I am developing a CMS myself and it took me ages to find a viable solution to this. Let me explain myself to make sure that we are on the same page here, but I am fairly certain that we area.
Your URLS can be formatted the following ways:
http://www.mydomain.com/about - a top level page with no category
http://www.mydomain.com/services/maintenance - a page with a parent category
http://www.mydomain.com/services/maintenace/server-maintenance - a page with a category and sub category.
In my pages controller I am using the _remap function that basically captures all requests to your controllers and lets you do what you want with them.
Here is my code, commented for your convenience:
<?php
class Pages extends Controller {
// Captures all calls to this controller
public function _remap()
{
// Get out URL segments
$segments = $this->uri->uri_string();
$segments = explode("/", $segments);
// Remove blank segments from array
foreach($segments as $key => $value) {
if($value == "" || $value == "NULL") {
unset($segments[$key]);
}
}
// Store our newly filtered array segments
$segments = array_values($segments);
// Works out what segments we have
switch (count($segments))
{
// We have a category/subcategory/page-name
case 3:
list($cat, $subcat, $page_name) = $segments;
break;
// We have a category/page-name
case 2:
list($cat, $page_name) = $segments;
$subcat = NULL;
break;
// We just have a page name, no categories. So /page-name
default:
list($page_name) = $segments;
$cat = $subcat = NULL;
break;
}
if ($cat == '' && $subcat == '') {
$page = $this->mpages->fetch_page('', '', $page_name);
} else if ($cat != '' && $subcat == '') {
$page = $this->mpages->fetch_page($cat, '', $page_name);
} else if ($category != "" && $sub_category != "") {
$page = $this->mpages->fetch_page($cat, $subcat, $page_name);
}
// $page contains your page data, do with it what you wish.
}
?>
You of course would need to modify your page fetching model function accept 3 parameters and then pass in info depending on what page type you are viewing.
In your application/config/routes.php file simply put what specific URL's you would like to route and at the very bottom put this:
/* Admin routes, login routes etc here first */
$route['(:any)'] = "pages"; // Redirect all requests except for ones defined above to the pages controller.
Let me know if you need any more clarification or downloadable example code.

Different version of the cache in Symfony

I'm new in Symfony and I have a problem with logical code organisation.
The problem is connected with cache and different version of webpage for guests, logged in users and owner.
For example. I have 'user' module, which has 'show' action, and the URL is /user/show/:id and URL is the same for every visitor. But the content of the page depends on visitor and is selected with 'if' conditions so... If I clear the cache and the first visitor is guest, then others (including owner and logged in users) will see the guest's cached page.
Some kind of solution can be separating each view (owner, guest, logged in user) to partial, but it's against the DRY rule.
How to do this?
You can use the sf_cache_key parameter. See here how. I think you could use the user_id for logged in user, prepended with an arbitrary string for the owner, and for the guests, the string "guest" would do.
A bit of pseudo-code to help you further:
$sf_cache_key = '';
if ($visitor->isLogged())
{
if ($visitor->getId() == $userId )
{
$sf_cache_key = 'owner' . $userId;
}
else
{
$sf_cache_key = 'logged_in' . $userId;
}
}
else
{
$sf_cache_key = 'guest' . $userId;
}
I'm sure you solved this by now, and the app is already upgraded to the latest version. But I solved a similar problem generically by including a filter that sets a user-specific parameter in every URL preventing the data leak. This destroys reporting in GA, which is my current problem.
// Filter class in apps/frontend/lib/accessFilter.class.php
<?php
class accessFilter extends sfFilter
{
public function execute($filterChain)
{
$context = $this->getContext();
$context->getRouting()->setDefaultParameter('sw_user_id', $user_id);
$filterChain->execute();
}
}
// Filter definition in apps/frontend/config/filters.yml
# insert your own filters here
accessFilter:
class: accessFilter
// Use within routes in apps/frontend/config/routing.yml
dashboard:
url: /dashboard/:sw_user_id/home
param: { module: dashboard, action: index }

Resources