Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

How to use CanCan when the permission has multiple conditions? #453

Description

@bhellman1

Ryan, I have a model that's permission is based on a variety of conditions. I've been going in circles to define the CanCan condition correctly and it continues to get very messy. I was wondering if you had a suggestion that might be helpful here?

Models:
User
Room (id, password_required (boolean)
UserRoom (user_id, room_id, banned (boolean)

User Stories:

  1. Any user can join a Room when password_required == false. Unless they have a UserRoom.banned == true record
  2. Any user can join a Room when they have a UserRoom record with banned == false
  3. User can not join a Room that is password required == true and they do not have an existing UserRoom record

So far I have:

def initialize(user, room, room_password)
.....
can :show, Room if room && user.try(:is_a_room_member, room) == true && user.try(:is_a_banned_room_member, room) == false

As you can see the above doesn't solve all the stories and it's already getting ugly.

Any suggestions on how this can be simplified/cleaner and work?

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions