Laravel 8: Problem creating and using a a new Permission - laravel

I have a Laravel 8 Project on my Local Windows PC. I uploaded the project to my shared web hosting on Dreamhost via zip file and copying the entire database to remote host. (I am unable to use Composer and php artisan commands on the remote server) I am using Spatie Roles & Permissions in my project.
Later I had to add a new permission 'holiday_vacation' in my project. I created the new permission using artisan commands on my local system I believe that when a new permission is created, it adds a new record in the permissions table and a when a user is given access to a specific permission, a record is added to the model_has_permissions table. I believe that no other table is changed during this process. The newly created 'holiday_vacation' permission works fine on my local system.
However, after I manually updated the remote tables (permissions and model_has_permissions), the remote system is unable to find the new permission (holiday_vacation). The following commands in a controller display error message, "There is no permission named holiday_vacation for guard web."
if(auth()->user()->hasPermissionTo('holiday_vacation') )
{
dd("Has access");
}
I am absolutely sure that the permissions table has the holiday_vacation permission as I copied the permissions and model_has_permissions tables from the local database to the remote one.
Google search on this issue talks about clearing permission cache (e.g. php artisan cache:forget spatie.permission.cache then php artisan cache:clear). Unfortunately, I can't execute php artisan commands on my shared hosting.
Can someone offer a workaround, please?

#BABAK ASHRAFI's comment did the trick, except that the command needed to be modified slightly. (Ref: https://spatie.be/docs/laravel-permission/v3/advanced-usage/cache)
app()->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();

Related

Storage symlink doesn't work with Laravel and Vue in Namecheap CPanel (LiteSpeed)

I am trying to access my image files stored inside storage/app/public/subfolder/ using symlink inside /public folder in my Laravel app. Everything works fine in my local setup and also in Jelastic Apache deployment, but it doesn't seem to work in CPanel with LiteSpeed. I am trying to access the files using the link /storage/subfolder/image.png but it is not accessible on CPanel deployment.
I tried creating and deleting storage symlink hundreds of times, but it didn't work. Below are few things I tried:
Deleting and creating symlink again and again using php artisan storage:link command.
Creating symlink using linux command ln -s ../storage/app/public storage inside public folder.
Running Artisan::call('storage:link') using a web route entry.
Redeploying and reconfiguring the app several times and creating storage symlink.
In all of these failed attempts I can verify that storage symlink is created every time inside public folder. I can navigate and view files from public/storage/ folder using CPanel terminal without any issues but can't access them in the deployed app.
I have exact same configuration in Jelastic Apache deployment and in my local deployment and there it is working without any issues.
UPDATE:
Additionally, I tried creating a storage directly manually inside public folder with the same structure and it worked. But symlink doesn't work.
UPDATE 2:
I found below error log while checking error logs from CPanel.
2022-11-06 05:07:07.342556 [ERROR] [1669834] [T0] [HTAccess] Failed to open [/home/user/my-project/public/storage/subfolder/.htaccess]: Permission denied
There is no .htaccess inside subfolder but it is there in my public folder.
What might be the issue?
After making linking you storage to your public path using storage:link you can access your folder :
'Storage_path('subfolder/image.png');'
After a long search, I finally found the cause.
Somehow the storage/app/public directory didn't have sufficient permissions. So, I gave permissions to that public folder recursively using chmod and everything works fine now.
The symlink was working fine but it was not working due to permission issues in the parent folder.
This is a bit weird as I have never manually altered permissions in any Laravel folders in any of my other deployments.

Configure default permission for Laravel `storage` directory for _newly_ created files

How do I configure Laravel such that newly created files and directories below ./storage become group writable?
The Laravel ./storage directory contains files which are dynamically generated during runtime of the application. For example, ./storage/framework/views contains compiled and cached versions of the Laravel Blade templates. The webserver runs as user and group apache:apache. Upon every upgrade of the application, I need to run the console command ./artisan optimize:clear to remove outdated, cached files. The admin user is also a member of the group apache, e.g. lets assume the account is my_user:apache.
Unfortunately, Laravel creates new files with permission 0640 and directories with permission 0750, i.e. they are only group-readable, but not group-writable. This means running ./artisan optimize:clear as my_user fails because group membership is not sufficient.

Creating symlink setting up Laravel 5.6 on a remote server

I'm setting up a Laravel 5.6 site in a remote shared hosting server.
I'm failing at creating the storage symlink. I'm executing a file with the following line (as Laravel documentation and other tutorials instruct):
symlink('/home/nataliag/laravel/storage/app/public', '/home/nataliag/public_html/storage');
this creates a symlink, but instead of getting my images with mydomain.com/img/..., i have to go mydomain.com/storage/img/....
what link the php artisan storage:link creates? because that works in local, and all my links are created following that structure

Creating Model in laravel with correct file permissions

I am having a hard time trying to figure out the issue.
I am creating a Model using this Artisan command,
Artisan::call('make:model',['name' =>'Models\\'.$module.'\\'.$model_name]);
For whatever reasons,
1.there is a lock icon in the file
2.owner of the file is www-data
3.the permission for the file is 644 but when I create the Model from CLI the permission of the file is 757.
It all depends who runs the code, in this case web-server runs the code, thus Apache or NGINX is the owner of newly created file (same goes for uploads).
You can change this using
umask
change user/group of web-server
Best option is to use artisan as its meant to be used, from command line.

problems after upgrading xampp to 1.7.7

i have upgraded my XAMPP server to 1.7.7, after that the file uploaded using the php move_uploaded_file function is not accessible from the network. It works OK in the server. But from a remote machine it shows there is not enough permission.I can see the files in the uploads folder but I can't copy,move, rename or preview it.
$isMove = move_uploaded_file ($_FILES['image']['tmp_name'],'uploads/'.$_FILES['image']['name']);
if I uses the copy function instead of the move_uploaded_file it works perfect. But i have used move_uploaded_files in many projects. Is there any fix for this problem.
Also a project using the zend optimizer is also not working after the upgrade.
I think that you need to set permissions of the directory so as PHP scripts can run with uid of user who has write permission or else in the case if the permissions are not such if they give write permission to user www-data they may give error.

Resources