# Check if user is in right group

**URL:** <https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244>\
**Category:** Rails\
**Created:** [January 28, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244 "2006-01-28T14:16:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/1 "2006-01-28T14:16:55Z")

</div>

What is the best way of checking this?

I have 3 tables:

groups  
users  
group\_members

class GroupMember \< ActiveRecord::Base  
belongs\_to :group  
belongs\_to :user  
validates\_uniqueness\_of :user\_id, :scope =\> “group\_id”

Then I want a method here that checks if a given user is a member of a  
given group. What is the right way of doing that?

def is\_member\_of(user, group)  
??  
end

Regards,  
Henrik

---

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/2 "2006-01-28T14:53:07Z")

</div>

If a member can be in more than one group I would use a  
has\_and\_belongs\_to\_many relationship. You create a table called  
groups\_users and in taht table you have primary keys set for user\_id  
and group\_id you can also add other fields into that table for  
permissions and anything else. Than you need to set foreign key  
relationships (also indexes) that reference the respective id’s in  
the other tables. The HABTM table does not have an id column in it.  
If a user can only belong to one group I would get rid of the  
group\_members table. This is something that I used in PHP allot but  
it is not needed on rails with the great relationship support. As far  
as a check to see if a user is a member of a group It depends on what  
you are looking to display? if it content specific to the group when  
they access the page call a find that has the constraint of looking  
for the group\_id in the users table (foreign key relationship of has  
one) or look in groups\_users table to pull the users for that  
particular group.

Andrew

---

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/3 "2006-01-28T15:11:18Z")

</div>

Thanks for the fast reply :-).

An user can be an member of a lot of groups.

The GoupMember class i supposed to do a lot of things. I therefore  
deliberately chose to use this class instead of HABTM (as recommended  
it the Aguile book “When a join wants to be a model”, page 241). The  
problem is that I just haven’t yet got the whole grasp of this magic  
here in AR.

I have started with:

def is\_member\_of(user, group)  
@user = User.find(:id,  
:condition =\> [“uname = ?”, user])  
@group = Group.find(:id,  
:condition =\> [“name = ?”, group])  
end

But I guess it is more clever ways of doing this in Rails. Beside I’m  
not sure how to continue in this method.

- Henrik  
Sat, 28 Jan 2006, Andrew F. skrev:

> as a check to see if a user is a member of a group It depends on what
> 
> > belongs\_to :user  
> > Henrik
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> Henrik Ormåsen

–  
SOS Rasisme er Norges eneste demokratiske medlemsorganisasjon som  
konsentrerer seg om å arbeide mot rasisme og nazisme. Vi har over  
40.000 individuelle medlemmer, 8.000 kollektive medlemmer og 90  
lokallag. Vi er Europas største antirasistiske organisasjon.  
Har du lyst til å bli medlem? Gå til: www.sos-rasisme.no/blimedlem  
Har du lyst til å bli aktiv? Gå til: www.sos-rasisme.no/bliaktiv

PB 9427, Grønland -0135 OSLO  
Besøk oss i Brugata 14, 0186 Oslo (Samme bygg som 7 Eleven)

telefon: 23 00 29 00  
direkte: 23 00 29 06  
faks: 23 00 29 01  
mobil: 48 18 14 92

---

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/4 "2006-01-28T16:14:41Z")

</div>

Hello Kevin!

Thanks for your advise. I have from before installed the old login  
generator. I should probably upgrade to this one.

But I’m still little unhappy about my missing understanding of the  
possibilities of this case. Even if the user|login\_engine will solve  
my problem here, I will eventually get the same problem later. I’m  
working with a news publishing system where the grouping system will  
be a very important thing.

But thanks anyway. I no body will help me with my silliness limiting  
my ability here, I can try to read some open code and see if I can get  
some out of it.

- Henrik  
Sat, 28 Jan 2006, Kevin O. skrev:

> If you want to only permit user assigned to a particular role to be able
> 
> :conditions =\> [“visible\_to = ?”, role\_id]
> 
> Actually… you would need to set up the query to pick up items in any  
> of the user’s roles, since they can have more than one.
> 
> That combination of controller/action access control and record level  
> control should cover most use cases.
> 
> \_Kevin

Henrik Ormåsen

–  
SOS Rasisme er Norges eneste demokratiske medlemsorganisasjon som  
konsentrerer seg om å arbeide mot rasisme og nazisme. Vi har over  
40.000 individuelle medlemmer, 8.000 kollektive medlemmer og 90  
lokallag. Vi er Europas største antirasistiske organisasjon.  
Har du lyst til å bli medlem? Gå til: www.sos-rasisme.no/blimedlem  
Har du lyst til å bli aktiv? Gå til: www.sos-rasisme.no/bliaktiv

PB 9427, Grønland -0135 OSLO  
Besøk oss i Brugata 14, 0186 Oslo (Samme bygg som 7 Eleven)

telefon: 23 00 29 00  
direkte: 23 00 29 06  
faks: 23 00 29 01  
mobil: 48 18 14 92

---

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/5 "2006-01-28T16:38:51Z")

</div>

Henrik,

if you want to keep the UserGroup model you first posted, how about

class User \< AR:B  
has\_many :group\_members

def member\_of?(group)  
group\_members.include? group  
end  
end

class Group \< AR:B  
has\_many :group\_members  
end

cheers  
Gerret

---

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/6 "2006-01-28T23:55:03Z")

</div>

Thanks everybody. I’ve converted to the user|login engine. It looks  
really nice, and I guess its anyway a good thing to have this part  
separated from rest of the app like this engine thing does. Nice to be  
able to update it without a lot of work :-).

Also thanks to Gerret, small thing when you know it :-).

- Henrik

---

<div class="post-metadata">

**Author:** ![Henrik\_SSiso-8859-1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/3be4f8/32.png) [@Henrik\_SSiso-8859-1](https://www.ruby-forum.com/u/Henrik_SSiso-8859-1)\
**Post date:** [January 28, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/check-if-user-is-in-right-group/98244/7 "2006-01-28T15:13:57Z")

</div>

Henrik =?iso-8859-1?Q?Orm=E5sen?= wrote:

> Then I want a method here that checks if a given user is a member of a  
> given group. What is the right way of doing that?

You can do a lot of this kind of stuff using ‘roles’ from the  
‘user|login\_engine’.

You can restrict access to specific controller/action pairs using that  
system (which works nicely IMO).

If you want to only permit user assigned to a particular role to be able  
to see an object, you would need some sort of association describing  
which roles can see the object. In some cases you could make it as  
simple as having a ‘visible\_to’ column and populating that with the id  
of the appropriate role.

Then you modify your find method to be something like…

@object = Object.find\_all\_by\_visible\_to(role\_id)

or add a condition like…

:conditions =\> [“visible\_to = ?”, role\_id]

Actually… you would need to set up the query to pick up items in any  
of the user’s roles, since they can have more than one.

That combination of controller/action access control and record level  
control should cover most use cases.

\_Kevin
