RBAC_CANCAN

57 views
Skip to first unread message

babacar thiam

unread,
Jul 13, 2017, 4:43:41 PM7/13/17
to Ruby on Rails: Talk
Hello guys i want to implement rbac autorisation for a rails app.
i use devise to generate user and i have a product controllers.
am a beginners at rails and i want to use:
user role show 
admin role edit
speruser role delete
I don't know how to implement this i use cancan and i have a problem to affect a role to a user

David Gleba

unread,
Jul 14, 2017, 6:44:52 AM7/14/17
to Ruby on Rails: Talk
I use devise and cancancan for login and roles.

In my little rails app generator project: https://github.com/dgleba/bashrail  there are scripts for this. Even if you don't use the scripts, they document exactly how to implement it in that case.

look at:  

When I am learning something like this I strip the scripts down to just what is needed to create an app with the feature I am studing.

This app, https://github.com/dgleba/brail351d, was generated by bashrail and has fully functional roles like mention in your question.

David
Message has been deleted

babacar thiam

unread,
Jul 17, 2017, 7:38:30 PM7/17/17
to Ruby on Rails: Talk
i want to use your script to test how implement robe based access control .
I hav a problems to import this or to add some file  in my projet.
I can't see the views who create user our assign a role to a user its my matter.
help need if i could have our contact to communicate fastly.
need u david Dleba 

Le lundi 17 juillet 2017 18:46:21 UTC-4, babacar thiam a écrit :
i look our script AND I COME WITH U IF I HAVE NEW

David Gleba

unread,
Jul 17, 2017, 11:07:17 PM7/17/17
to Ruby on Rails: Talk
What operating system are you using?

Can you download the bashrail project to your PC and follow the instructions in the README.md. 

I wonder if you can run it as it is and see if it generates a rails app you can run. Then we can break it down into smaller pieces and try to understand what it is doing.

babacar thiam

unread,
Jul 17, 2017, 11:22:27 PM7/17/17
to Ruby on Rails: Talk
I use mac os x.
I want to use your app to see specifically how use Rbac in a simple app.
And in my app i don't undertand how to link user to a specifique role.
in devise/ folder i can't see an in the form how to add role id 
and if i want to add user in the seed file i have a error.

d gleba

unread,
Jul 17, 2017, 11:29:07 PM7/17/17
to rubyonra...@googlegroups.com
The role_id is a column in the users table.

The role.rb model and the user.rb model are related.


In user.rb the roles are processed.

I find it is helpful to try to add the feature you want to a fresh plain test project.

When you can add it to a test project, then try to add it to your project.



--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/oHSqL1aYLs0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/75f291b1-a395-4224-87a0-7ed0b2eeccf4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

David Gleba

unread,
Jul 17, 2017, 11:34:39 PM7/17/17
to Ruby on Rails: Talk, dgl...@gmail.com
Also, my implementation was patterned after this article.


Maybe that will help you also.

Can you post specific error messages, screen shots, code, etc? I may be able to answer specifically.

babacar thiam

unread,
Jul 17, 2017, 11:41:50 PM7/17/17
to Ruby on Rails: Talk, dgl...@gmail.com
yeah i see this moment i create a simple web app i use devise and cancancan
but i cant creer a select form to insert role id in the database for using html files devise/registrations.
In your github i dont undertand how to use its when i import then i have many thing aborted
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

babacar thiam

unread,
Jul 18, 2017, 12:07:07 AM7/18/17
to Ruby on Rails: Talk, dgl...@gmail.com
I go to create a teste app  using https://hibbard.eu/authentication-with-devise-and-cancancan-in-rails-4-2/ 
simple user project and role.
If it will run normally i could contact you else i i give you our mistake 

babacar thiam

unread,
Jul 18, 2017, 5:02:48 PM7/18/17
to Ruby on Rails: Talk, dgl...@gmail.com
hi i have a mistake since this morning .

undefined method `name' for nil:NilClass

Extracted source (around line #15):
13
14
15
16
17
18
              
end
def admin?
self.role.name == "Admin"
end
def seller?
self.role.name == "Seller"

Colin Law

unread,
Jul 18, 2017, 5:15:51 PM7/18/17
to Ruby on Rails: Talk
On 18 July 2017 at 22:02, babacar thiam <thia...@gmail.com> wrote:
>
> hi i have a mistake since this morning .
>
> undefined method `name' for nil:NilClass
>
> Extracted source (around line #15):

>
> end
> def admin?
> self.role.name == "Admin"
> end
> def seller?
> self.role.name == "Seller"
>

As the error says, self.role is nil

Colin

babacar thiam

unread,
Jul 18, 2017, 7:07:11 PM7/18/17
to Ruby on Rails: Talk
the error says no MethodError (undefined method `name' for nil:NilClass):

  

app/models/user.rb:15:in `admin?'

app/models/ability.rb:7:in `initialize'

David Gleba

unread,
Jul 18, 2017, 10:28:33 PM7/18/17
to Ruby on Rails: Talk
I see that in one spot you have Admin - capitalized. and in another it is not capitalized - admin.

I wonder if capitalization matters in your code.

Note that we can't see your code.  That makes it harder to help you when we can't see the code.

David Gleba

unread,
Jul 18, 2017, 10:47:12 PM7/18/17
to Ruby on Rails: Talk
The article has an app in github.


Can you download it and run it successfully?

babacar thiam

unread,
Jul 19, 2017, 12:06:09 AM7/19/17
to Ruby on Rails: Talk
yeah in this i want to know how do to insert a new users using user view or devise and respected role.
in my userscontroller if i put at the top: before_filter :authentificate_user i hav and error saying 
use before_action 

Colin Law

unread,
Jul 19, 2017, 3:15:48 AM7/19/17
to Ruby on Rails: Talk
On 19 July 2017 at 00:07, babacar thiam <thia...@gmail.com> wrote:
> the error says no MethodError (undefined method `name' for nil:NilClass):

Exactly. The error says you have tried to call method 'name' on
something that is nil. You are calling method 'name' on self.role (you
have used self.role.name) therefore the error is saying that self.role
is nil.

Colin

>
>
>
> app/models/user.rb:15:in `admin?'
>
> app/models/ability.rb:7:in `initialize'
>
>
> Le mardi 18 juillet 2017 17:15:51 UTC-4, Colin Law a écrit :
>>
>> On 18 July 2017 at 22:02, babacar thiam <thia...@gmail.com> wrote:
>> >
>> > hi i have a mistake since this morning .
>> >
>> > undefined method `name' for nil:NilClass
>> >
>> > Extracted source (around line #15):
>>
>> >
>> > end
>> > def admin?
>> > self.role.name == "Admin"
>> > end
>> > def seller?
>> > self.role.name == "Seller"
>> >
>>
>> As the error says, self.role is nil
>>
>> Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/e7a9e5c3-d026-495e-914d-fa26bcf39f8a%40googlegroups.com.

David Gleba

unread,
Jul 19, 2017, 6:47:20 AM7/19/17
to Ruby on Rails: Talk
I may not be answering your question on this one, but I just an admin tool to add a user since the administrator of the system must grant the proper role to the new user. 
So, if am using rails_admin with the project, add the user in that tool and assign the appropriate role_id.

The code can also be setup so a user can sign-up them selves and a default role is assigned.

You can also add a user with a tool such as phpmyadmin, pgadmin, sqlite-browser like http://sqlitebrowser.org/.

babacar thiam

unread,
Jul 19, 2017, 2:47:20 PM7/19/17
to Ruby on Rails: Talk
yeah its great david i havent problem if i had a user in my seed file (for example) and then i need to add new role new user or new item for my html.erb file and i don't know how i do to link user and item if i add item example.
Second wan't to know if its possible to use your projet (david ) to verify something for my user role and admin 
(need to import new projet and doing test for this) 

David Gleba

unread,
Jul 19, 2017, 7:07:46 PM7/19/17
to Ruby on Rails: Talk
For your second item:

I suggest that you download this:  https://github.com/dgleba/brail347a22

  • cp  config/database-example.yml    config/database.yml   
  • cp  config/application-example.yml config/application.yml
  • cp  config/secrets-example.yml     config/secrets.yml    

  • Check db/../..seeds*.rb for username and password.
  • bundle
  • rails s
It was recently created by bashrail.


For your first item: 
If am answering your question, I would scaffold User2 to match the User resource.
Then, change the table name in user2.rb to the user table.

Like so,  
self.table_name = 'users'

This will allow you to edit the users table from user2/ url.

I will add this to bashrail when I get a chance.

babacar thiam

unread,
Jul 19, 2017, 7:31:30 PM7/19/17
to Ruby on Rails: Talk
I see your git i check this 2 item and i import your implementation for testing.
for the first step my friend say me to use this git https://github.com/605data/strongbolt_example and for me i have this information for your git brail.
You suggest me the strong bolt guide or your example.
For the first item 1:
i see the problem was solved. In a simple example if i add user or item or role at the seed.rb file it run normally and me i need to add user or item in the html.erb file? 

David Gleba

unread,
Jul 19, 2017, 7:59:42 PM7/19/17
to Ruby on Rails: Talk

1:
So, are you saying you have succeeded with:  'i see the problem was solved. In a simple example if i add user or item or role at the seed.rb file it run normally and me i need to add user or item in the html.erb file? '

2:
It is your choice. Devise/cancancan or strongbolt.
I have not looked at strongbolt.
I am having good results with Devise/cancancan.

babacar thiam

unread,
Jul 19, 2017, 8:09:47 PM7/19/17
to Ruby on Rails: Talk
I say that i have a mistake if i want to create new user or new role or new item in the html.erb.
And if i use seed.rb i succeeded.
2.
Devise/cancancan i think that its interesting so i look for other alternative for cancancan.

David Gleba

unread,
Jul 19, 2017, 9:06:35 PM7/19/17
to Ruby on Rails: Talk

babacar thiam

unread,
Jul 19, 2017, 9:20:52 PM7/19/17
to Ruby on Rails: Talk
Yeah its my blem to create this.
Cause for me i have a view user and if i want to creer or edit new view i havent role my collection select is empty.
MAy i import your view user2 for me ?

David Gleba

unread,
Jul 19, 2017, 9:26:01 PM7/19/17
to Ruby on Rails: Talk
Do this in your project folder at command prompt.


rails generate scaffold User2 email:string encrypted_password:string reset_password_token:string reset_password_sent_at:datetime remember_created_at:datetime sign_in_count:integer current_sign_in_at:datetime last_sign_in_at:datetime current_sign_in_ip:string last_sign_in_ip:string confirmation_token:string confirmed_at:datetime confirmation_sent_at:datetime unconfirmed_email:string failed_attempts:integer unlock_token:string locked_at:datetime name:string role:references --no-migration



###  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# add new lines of text after patrn...

filetarg='app/models/user2.rb'
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
  repl2 = %Q{
  self.table_name = 'users'
  }
  ARGF.each do |line|
    puts line
    puts repl2 if line =~ /ApplicationRecord/
  end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp
cp $filetarg.tmp $filetarg; rm $filetarg.tmp

###  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  

babacar thiam

unread,
Jul 19, 2017, 9:45:53 PM7/19/17
to Ruby on Rails: Talk
Yeah i see and then if i wan t to load data at item html.erb i have a problem for my user id because i have a link to user and item in item table.

babacar thiam

unread,
Jul 19, 2017, 11:17:21 PM7/19/17
to Ruby on Rails: Talk
Hi in my item view i want to know how to add item using the user id who connected in my app i doo link user to item

babacar thiam

unread,
Jul 20, 2017, 12:42:00 AM7/20/17
to Ruby on Rails: Talk
hi all i want  to solve my problem i need to download a minimal projet and if i bundle them i have a mistake saying.

Reply all
Reply to author
Forward
0 new messages