I have many levels of user permissions in my site.
So a junior user can NOT do things that a senior user can do. ( there are many levels of users. senior vs junior is just for simplification).
In addition to a server side validation for who is the user and what he can/t do — I also want NOT to provide the Javascript function registration at first place . (So if a user is a junior user , then his JS files should not contain JS functions of a senior user.)
Example :
The site has a general add javascript function which should appear to all users:
function add() {
console.log("Add");
}
Junior users has higher privilege so they can ALSO do subtract . so their JS files include the subtract method:
function substract() {
console.log("substract");
}
A general users won't have this substract method
Senior users has higher privilege so they can ALSO do power. so their JS files include the power method :
function power() {
console.log("power");
}
A senior user won't have this power method
OK
According to this answer :
Note that you must have a separate bundle per page. You should not
modify one and the same bundle on the fly....etc...
So , I created ( for now) 2 bundles , one for each page :
bundles.Add(new ScriptBundle("~page1Junior").Include(
"~/Scripts/General.js", "~/Scripts/Junior.js"));
bundles.Add(new ScriptBundle("~page1Senior").Include(
"~/Scripts/General.js", "~/Scripts/Senior.js"));
And then I take the relevant bundle according to some login and use :
<script src='#Scripts.Url("~/page1Senior (or) Junior")' async> </script>
Question
This seems like a wrong way to go.
If I have 10 pages and 10 user levels , According to that approach , I will need 100 different bundles.
what is the right way of serving appropriate js files for their relevant user permissions ?
Pseudo code of what i'm after :
user level js files(combined to one)
------------|-------------
1 [a,b]
2 [a,b,c]
3 [a,b,c,d]
4 [a,b,c,d,e]
So If a user level 3 is logged on , then a single abcd.js JS file should be served ( minified).
One way is to use DynamicBundleFolder
If you can have a directory structure like
Scrtips
-- /Permissions
-- /1
-- a.js
-- b.js
-- /2
-- a.js
-- b.js
-- c.js
-- /3
-- c.js
-- d.js
where 1,2,3 are the restriction levels, then in your RegisterBundles add a DynamicBundle
bundles.Add(new DynamicFolderBundle("userScripts", "*.js",new JsMinify()));
And then in your _layout.cshtml, you can do
#{
var userPermissionLevel = 2; // grab your user permission level, from session maybe
}
#Scripts.Render("~/Scripts/Permissions/"+ #userPermissionLevel +"/userScripts")
and that will render all the scripts in that directory. You dont have to create bundles for each page then. And if you need to add new permission based file, just add to respective permission folder.
Update: This will work fine if you have unique js files for each permission level. For shared files, updating them will be quite a work. So what you can do is create bundle according to permission levels like
// Bundle for permission level 1
bundles.Add(new ScriptBundle("~/bundles/permissions/1").Include(
"~/Scripts/a.js")
.Include("b.js"));
// Bundle for permission level 2
bundles.Add(new ScriptBundle("~/bundles/permissions/2").Include(
"~/Scripts/b.js")
.Include("c.js"));
// and so on for each level 3,4,5 include specific level files
and then again you can add them to page throught _layout.cshtml
#{
var userPermissionLevel = 2; // grab your user permission level, from session maybe
}
#Scripts.Render("~/bundles/permissions/"+userPermissionLevel)
Related
In the src/AppBundle/Controller folder, there is a file called DefaultController.php.
I'll create url's like below, should I use just DefaultController.php for all URL requests or is it recommended to use a different controller.php file (UserController.php, FeedController.php, etc) for each part of the site? (profile, feed, settings, etc)
I also have another question. As far as I understand, we put our html files in our /App/Resources/views folder to keep them separated. Do I need to create a specific file for each part of the website just like flat PHP? (settings/index.php, settings/password.php, settings/things.php, settings/security.php, etc).
I am not sure whether this question is suitable for SO or not.
settings
/settings
/settings/password
/settings/things
/settings/security
/settings/privacy
/settings/ban
/settings/notifications
/settings/mail
/settings/mobile
/settings/applications
/settings/advertising
/settings/invite
user
/username
/username/photos
/username/friends
/username/posts
feed
/feed
/feed/posts/postid
For both questions is no hard answer. I should create a controller for each part of your website AT LEAST. Theoretical you could throw everything into one controller but it will be a very long list if you are finished. Another problem is that your action names like indexAction will repeat which is of course not possible because every method must have a different name. And names like index1Action, index2Action and so on is also not a proper solution :-). Another helper is to create an own controller for every ENTITY.
Twig files should only be written for one page only or only for a part of a page. Imagine that you have a homepage with last 10 newsitems but also a news page with more news items (maybe with pagination). The newsitems themselves looks the same on both pages. In this case you could make a home.html.twig, a news.html.twig and also a newsitem.html.twig. Both home and news will include newsitem to show the newsitems...
Hope i gave you a light.
I'm working on custom Drupal8 module. My module uses this routing file:
kalvis.routing.yml
kalvis.content:
path: '/kalvis/{from}/{to}'
defaults:
_controller: '\Drupal\kalvis\Controller\kalvisController::content'
_title: ''
requirements:
_permission: 'access content'
What does _permission part stand for and where can I find a list of all possible values for this parameter?(in tut's I've watched were used only access content and access administrative content but I suppose there is a lot more of them)
PS: I'm using Drupal 8 beta 10 installed on WAMP
If you want to see a list of all permission, the code below should work. work. If you are coding your own module you can define your own permissions and test if a user has a role with that permission.
function my_module_page_attachments_alter(array &$attachments) {
$perms = array_keys(\Drupal::service('user.permissions')->getPermissions());
}
To answer the question what is the _permission part of the routing structure. Here is a quote from the drupal docs about what it does.
_permission: A permission string (e.g., _permission: 'access content'). You can specify multiple permissions by separating them with ',' (comma) (e.g., _permission: 'access content,access user profiles') for AND logic or '+' (plus) for OR logic (e.g., _permission: 'access content+access user profiles' means a visitor needs either the access content permission or the access user profiles permission to view the page. Having both is fine, too.). Module-specific permission strings can be defined in my_module_name.permissions.yml. See hook_permission() replaced with permissions defined in a my_module_name.permissions.yml file for details.
source: https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-routes
To put it simply this restricts access to this route by only allowing users with the specified permission(s) to access it. To use it you need to know the system name of the permission(s) you want to use to restrict access. Then you just place then as a string behind this paramerter. Like in the quote above. You can choose to use multiple permissions by separating them with , for AND logic or + for OR logic. Permissions system names are allowed to have spaces in them and frequently do.
I don't think there is any way to directly see it in ui if you are talking about the system names of the permissions. You can ofcource see all permissions on www.site.com/admin/people/permissions. If you are in a hurry and/or looking for a specific permission you can always look through the module.permissions.yml file of the module this permission is defined in.
If you do want to see all permissions you can make your own list of all the system names.
You can use the PermissionHandler service from the core module.
This does the following gets all yaml's and creates a list.
You would call this by calling Drupal::service('user.permissions')->getPermissions() (https://api.drupal.org/api/drupal/core%21modules%21user%21src%21PermissionHandler.php/function/PermissionHandler%3A%3AgetPermissions/8.2.x)
You can use or try to write similar code to the functionality of the user_role_permissions function from the user.module file in drupal core. It looks like this:
function user_role_permissions(array $roles) {
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
return _user_role_permissions_update($roles);
}
$entities = Role::loadMultiple($roles);
$role_permissions = array();
foreach ($roles as $rid) {
$role_permissions[$rid] = isset($entities[$rid]) ? $entities[$rid]
->getPermissions() : array();
}
return $role_permissions;
}
This code as you can see just loads all the role entities with loadMultiple (although technically you should use the entitytypemanager to load the entities whenever possible like $entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple([1, 2, 3]); for more information see the drupal entity api (https://www.drupal.org/docs/drupal-apis/entity-api/working-with-the-entity-api)).
After loading all the roles it makes a list of all permissions.
Source information below. This should stay up to date because drupal keeps their documentation versioned. But because comments suggested it I figured I might as well write it out to save you some clicks.
Original drupal documentation.
https://api.drupal.org/api/drupal/core!modules!user!user.module/function/user_role_permissions/8.2.x
Hope this helps! :)
You can confirm in the page '/admin/people/permissions'.
A quick and dirty way to see them is to create a View with a Page display. Then in the 'Access' section, ensure 'Permission' is selected and open up the options as if you were going to choose a different permission.
You can now inspect the HTML of the <select> element, the Ids of each option is the correct name for each permission:
I am working on Nginx server, with PHP-FPM. I installed Laravel 4.1 and bootstrap v3.1.1., and here is the problem. For the last 30 minutes, I have been trying to change a css rule that I first declared to check boostrap.
.jumbotron{
background: red;
}
The first time it worked. The jumbotron container was red. So, I removed that css value and started working, but still no matter which browse I use, the container is red. I even checked the css file through the Google Chromes inspection tool, and it is showing me that first value when jumbotron had a background:red. I deleted the css file and renamed it and add new styles, I configured chrome not to cache pages. But Still the same value. I'm convinced now, that Laravel has kept a cache of the first style declaration.
Is there any way to disable this at all?
General explanation
When you access a Laravel Blade view, it will generate it to a temporary file so it doesn't have to process the Blade syntax every time you access to a view. These files are stored in app/storage/view with a filename that is the MD5 hash of the file path.
Usually when you change a view, Laravel regenerate these files automatically at the next view access and everything goes on. This is done by comparing the modification times of the generated file and the view's source file through the filemtime() function. Probably in your case there was a problem and the temporary file wasn't regenerated. In this case, you have to delete these files, so they can be regenerated. It doesn't harm anything, because they are autogenerated from your views and can be regenerated anytime. They are only for cache purposes.
Normally, they should be refreshed automatically, but you can delete these files anytime if they get stuck and you have problems like these, but as I said these should be just rare exceptions.
Code break down
All the following codes are from laravel/framerok/src/Illuminate/View/. I added some extra comments to the originals.
Get view
Starting from Engines/CompilerEngine.php we have the main code we need to understand the mechanics.
public function get($path, array $data = array())
{
// Push the path to the stack of the last compiled templates.
$this->lastCompiled[] = $path;
// If this given view has expired, which means it has simply been edited since
// it was last compiled, we will re-compile the views so we can evaluate a
// fresh copy of the view. We'll pass the compiler the path of the view.
if ($this->compiler->isExpired($path))
{
$this->compiler->compile($path);
}
// Return the MD5 hash of the path concatenated
// to the app's view storage folder path.
$compiled = $this->compiler->getCompiledPath($path);
// Once we have the path to the compiled file, we will evaluate the paths with
// typical PHP just like any other templates. We also keep a stack of views
// which have been rendered for right exception messages to be generated.
$results = $this->evaluatePath($compiled, $data);
// Remove last compiled path.
array_pop($this->lastCompiled);
return $results;
}
Check if regeneration required
This will be done in Compilers/Compiler.php. This is an important function. Depending on the result it will be decided whether the view should be recompiled. If this returns false instead of true that can be a reason for views not being regenerated.
public function isExpired($path)
{
$compiled = $this->getCompiledPath($path);
// If the compiled file doesn't exist we will indicate that the view is expired
// so that it can be re-compiled. Else, we will verify the last modification
// of the views is less than the modification times of the compiled views.
if ( ! $this->cachePath || ! $this->files->exists($compiled))
{
return true;
}
$lastModified = $this->files->lastModified($path);
return $lastModified >= $this->files->lastModified($compiled);
}
Regenerate view
If the view is expired it will be regenerated. In Compilers\BladeCompiler.php we see that the compiler will loop through all Blade keywords and finally give back a string that contains the compiled PHP code. Then it will check if the view storage path is set and save the file there with a filename that is the MD5 hash of the view's filename.
public function compile($path)
{
$contents = $this->compileString($this->files->get($path));
if ( ! is_null($this->cachePath))
{
$this->files->put($this->getCompiledPath($path), $contents);
}
}
Evaluate
Finally in Engines/PhpEngine.php the view is evaluated. It imports the data passed to the view with extract() and include the file with the passed path in a try and catch all exceptions with handleViewException() that throws the exception again. There are some output buffering too.
Same issue here. I am using VirtualBox with Shared Folders pointing to my document root.
This pointed me in the right direction:
https://stackoverflow.com/a/26583609/1036602
Which led me to this:
http://www.danhart.co.uk/blog/vagrant-virtualbox-modified-files-not-updating-via-nginx-apache
and this:
https://forums.virtualbox.org/viewtopic.php?f=1&t=24905
If you're mounting your local dev root via vboxsf Shared Folders, set EnableSendFile Off in your apache2.conf (or sendfile off if using Nginx).
For what it's worth and because this answer came up first in my google search...
I had the same problem. The CSS and JS files wouldn't update. Deleting the cache files didn't work. The timestamps were not the problem. The only way I could update them was to change the filename, load it directly to get the 404 error, and then change the name back to the original name.
In the end the problem was not related to Laravel or the browser cache at all. The problem was due to NginX using sendfile which doesn't work with remote file systems. In my case, I was using VirtualBox for the OS and the remote file system was vboxsf through Guest Additions.
I hope this saves someone else some time.
In Laravel 5.8+ you can use so:
The version method will automatically append a unique hash to the filenames of all compiled files, allowing for more convenient cache busting:
mix.js('resources/js/app.js', 'public/js').version();
After generating the versioned file, you won't know the exact file name. So, you should use Laravel's global mix function within your views to load the appropriately hashed asset. The mix function will automatically determine the current name of the hashed file:
<script src="{{ mix('/js/app.js') }}"></script>
full document: https://laravel.com/docs/5.8/mix
I'm writing a little blog app where the user can publish public and private news. Users can attach files to these news. I have two contexts for this app: public_news, with files which can be accessed by everyone; and private_news, with files which can only be accessed if the user has log on.
I want to be able to move files from the public_news context to the private_news context when the user changes a news from public to private, and vice versa.
I was hoping to do something as simple as $media->setContext('private_news');, but this won't move the physical file from one directory to the other.
What do you think about recreating this media?
$oldMedia = getYourOldMedia();
// $media = clone($oldMedia); # For me it didn't work as expected
# YMMV - I didn't spend lots wondering about that
$media = new Media();
// This will work fine with image and file provider,
// but it was not tested with other providers
$pool = $container->get('sonata.media.pool');
$provider = $pool->getProvider($oldMedia->getProviderName());
$media->setBinaryContent($provider->getReferenceFile($oldMedia));
}
$media->setProviderName($oldMedia->getProviderName());
$media->setContext('private_news');
/* copy any other data you're interested in */
$mediaManager->save($media);
$mediaManager->delete($oldMedia);
$mediaManager->delete might not delete your physical files depending on provider, you might want to create your own provider if you wish to do so.
Edit:
On further research I found out that you can manualy delete your files before deleting old media:
if ($pool->getFilesystem()->has($path)) {
$pool->getFilesystem()->delete($path);
}
But don't do that before saving your new media entity.
I'm thinking of learning the ASP.NET MVC framework for an upcoming project. Can I use the advanced routing to create long URLs based on the sitemap hierarchy?
Example navigation path:
Home > Shop > Products > Household > Kitchen > Cookware > Cooksets > Nonstick
Typical (I think) MVC URL:
http://example.com/products/category/NonstickCooksets
Desired URL:
http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick
Can I do this?
Zack, if I understand right you want unlimited depth of the subcategories. No biggie, since MVC Preview 3 (I think 3 or 4) this has been solved.
Just define a route like
"{controller}/{action}/{*categoryPath}"
for an url such as :
http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick
you should have a ShopController with a Products action :
public class ShopController : Controller
{
...
public ActionResult Products(string categoryPath)
{
// the categoryPath value would be
// "household/kitchen/cookware/cooksets/nonstick". Process it (for ex. split it)
// and then decide what you do..
return View();
}
The MVC routing lets you define pretty much any structure you want, you just need to define what each of the pieces mean semantically. You can have bits that are "hard-coded", like "shop/products", and then define the rest as variable, "{category}/{subcategory}/{speciality}", etc.
You can also define several routes that all map to the same end point if you like. Basically, when a URL comes into your MVC app, it goes through the routing table until it finds a pattern that matches, fills in the variables and passes the request off to the appropriate controller for processing.
While the default route is a simple Controller, Action, Id kind of setup, that's certainly not the extent of what you can do.