Created
February 17, 2014 15:52
-
-
Save mustmodify/9053077 to your computer and use it in GitHub Desktop.
handling admin and user-lever permissions in fairly large apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module User::Abilities | |
# this is included in User | |
# | |
# class User < AR:B | |
# include Abilities | |
# end | |
# | |
def can_create?( object ) | |
admin? || | |
case object | |
when Certification, Membership, etc. | |
object.physician == self.physician | |
when Physician | |
object.email == self.email | |
else | |
false | |
end | |
end | |
def can_read?( object ) | |
admin? || | |
case object | |
when User | |
object.id = self.id | |
end || | |
true | |
end | |
def can_update?( object ) | |
admin? || | |
case object | |
when Certification, Membership, etc | |
object.physician == self.physician | |
when Physician | |
object.email == self.email | |
else | |
false | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MembershipsController | |
# POST /memberships | |
# POST /memberships.json | |
def create | |
@membership = Membership.new(membership_params) | |
if !current_user.can_create?(@membership) | |
go_away | |
elsif @membership.save | |
# render whatever | |
else | |
# render whatever | |
end | |
end | |
# PATCH/PUT /memberships/1 | |
# PATCH/PUT /memberships/1.json | |
def update | |
if !current_user.can_update?( @membership ) | |
go_away | |
elsif @membership.update(membership_params) | |
# render whatever | |
else | |
# render whatever | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment