cancancan | The authorization Gem for Ruby on Rails | Authorization library
kandi X-RAY | cancancan Summary
kandi X-RAY | cancancan Summary
Wiki | RDocs | Screencast 1 | Screencast 2. CanCanCan is an authorization library for Ruby and Ruby on Rails which restricts what resources a given user is allowed to access. All permissions can be defined in one or multiple ability files and not duplicated across controllers, views, and database queries, keeping your permissions logic in one place for easy maintenance and testing.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Human readable string
- Returns true if the condition matches the conditions .
- Matches the conditions according to the conditions hash .
- Sanitize parameters
- Recursively walk the resource
- Checks if conditions matches the conditions
- Generate a Hash of associations
- Computes an array of values
- Parses an object into an object .
- Find resource by name
cancancan Key Features
cancancan Examples and Code Snippets
Community Discussions
Trending Discussions on cancancan
QUESTION
I am trying to "extend" Rolify functionality to have some global roles such as 'Admin', 'Member', 'Guest', etc... and to be able to set up different "scopes" for each user who have a specific role.
For example, in my app i have this admin
role, which is a "super role" meaning it grants access to basically everything. But i also want to be able to "scope" this role for another User, the scope will be, for example 'he will have access to all users, but only if they are from countries A, B, C and from cities X, Y, Z'. I know rolify supports different roles with different scopes, but what i want is to manage "global roles" with different scopes only for different users.
I thought about doing something like a 'Scope' model that belongs to a Role and to a User, in which i would have HABTM relationships with countries and cities, and then use that for authorization (I'm using CanCanCan). But i ran into many issues when working on this approach. It was something like:
...ANSWER
Answered 2022-Feb-20 at 14:37If you want to create something of your own has_and_belongs_to_many
is not the answer (hint: it's almost never the right answer). Using HABTM is the akilles heel of Rolify as its assocations look like this:
QUESTION
I'm seeing the following error it only is appearing in cron jobs using the whenever gem. The application is working correctly otherwise. The scheduled job doesn't run. But I can run it manually and it does work.
...ANSWER
Answered 2021-Sep-23 at 15:08The issue was related to environment variables and not being able to find the correct path for the gems. I found a solution and updated the schedule.rb file.
QUESTION
In a Rails 5 application, I have a shortlist
model in a HABTM relationship with a user
model, with the users controlled by Devise. This is all working as expected, where each User can see their own Shortlists (only).
ANSWER
Answered 2021-Sep-09 at 06:22As a workaround I added an owner
flag on the shortlists, which is calculated to current_user.id
when the shortlist is created. This allows me to distinguish between the owner and viewers.
Before saving a shortlist I check whether @shortlist.owner == current_user.id
and show an error message if not.
This works well enough but I'd be keen to hear if there's a method that's less hacky.
QUESTION
controller.rb
...ANSWER
Answered 2021-Aug-01 at 10:59I figured it out:
QUESTION
I read somewhere using Rolify/Devise/Cancancan was a better alternative to configuring two devise models with login functionality (using one login page instead of two) and their respective associations between other models. I'm confused as to how to set roles within and still use associations. For instance:
If I had used two Devise models they would have just been...
...ANSWER
Answered 2021-Jul-01 at 19:29What you're looking for is most likely a self-referential association:
QUESTION
I have the user table (created by devise), I added a role column, which can be: admin, manager and agent. And in user.rb I put an ENUM like this:
...ANSWER
Answered 2021-Jun-27 at 23:24You can use the safe navigation operator to avoid a nil error:
QUESTION
Im running ruby version 2.6.1 with docker. Rake gem is version 13.0.1.
Whenever I tried docker-compose up, it always fails and throws this error everytime:
This error did not exist before.
ANSWER
Answered 2021-May-23 at 12:27I'm not really sure what happened and why but I tried doing this on my rails container and I was no longer receiving the said error.
docker-compose run --rm bash
cd to project directory
bundle install
QUESTION
I need to upload external directory references (json file) to my users data. It's a good opportunity to learn how to create an API and use cURL, but I still meet at least one issue.
I start with a single element, but I plan to pass a json data file at the end of the story. Typical elements to load look like this:
{"name": "Test", "id": "35", "external_id": "X-001"}
I went through the cURL manual and several StackOverflow posts to finally build this API:
Created a dedicated route in routes.rb
...ANSWER
Answered 2021-Apr-29 at 14:04I recommend that you avoid the JSON syntax problems and escaping needed for the shell by putting the JSON input into a file. If the file is named params.json
then use -d @params.json
to pass it from a file.
As for authentication, I'm not sure this is a good idea either but you might find a session key and pass it in the cookie header. If you are using database sessions (which is a good idea) then it will be the value in the column for the session in your database. If not then use dev tools and get your session from the browser.
QUESTION
Background: 'Add User' portion of an app works perfectly on two separate development machines, running two different OSes: Dev Machine 1 is a Mac running Catalina (10.15), Dev machine 2 is running an Ubuntu derivative (Pop!_OS 19.10). After pushing to a Digital Ocean Dokku server the 'Add User' functionality fails without error; hitting the submit button does nothing. 'Edit User' works in production, as well as dev.
The code is being managed via git, with no complications on that part. Therefore, the identical code is on three machines.
I rely on Devise for my user setup; however, skip registrations in order to restrict the public from creating an account. Instead I rely on cancancan with a role to create users. This all works in development, but I cannot see a proper error in production, it simply does a render :action => :new with no explicit error. I use Dokku, Rails 6.03 with Ruby Version 2.7.1
Development Log
...ANSWER
Answered 2021-Jan-24 at 21:04If anyone has any insight as to why this worked in development but not production, I'd love to know. Seems like it had to do with not loading Devise first.
The original code in routes.rb:
QUESTION
After upgrading my application from rails 5.2 to rails 6 and I am getting this issue
This model adapter does not support fetching records from the database.
...ANSWER
Answered 2021-Jan-06 at 04:27As suggested by @Eyeslandic I ended up updating cancancan from 2.3 to 3.0
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cancancan
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page