Created
November 20, 2010 22:53
-
-
Save iansheridan/708252 to your computer and use it in GitHub Desktop.
a user class with dynamic 'has role' methods
This file contains 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 Role | |
include DataMapper::Resource | |
has n, :users, :through => Resource | |
property :id, Serial | |
property :name, String | |
property :short_name, String | |
property :description, Text | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
default_scope(:default).update(:order => [:id]) # set default order | |
end |
This file contains 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 User | |
include DataMapper::Resource | |
has n, :roles, :through => Resource | |
has 1, :userinfo | |
property :id, Serial | |
property :login, String | |
property :full_time, Boolean, :default => true | |
property :rate_mod, Integer, :default => 0 | |
property :created_at, DateTime | |
property :updated_at, DateTime | |
validates_is_unique :login | |
# create on the fly the method to handle all the | |
# is_a_superuser? | |
def method_missing(method_id, *args) | |
if match = matches_dynamic_role_check?(method_id) | |
tokenize_roles(match.captures.first).each do |check| | |
return true if roles.collect{|r| r.short_name }.include?(check) | |
end | |
return false | |
else | |
super | |
end | |
end | |
private | |
def matches_dynamic_role_check?(method_id) | |
/^is_an?_([a-zA-Z]\w*)\?$/.match(method_id.to_s) | |
end | |
# might at some point add the _and_ token | |
def tokenize_roles(string_to_split) | |
string_to_split.split(/_or_/) | |
end | |
end |
This file contains 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 Users < Application | |
# The basic authentication method | |
before :ensure_authenticated | |
# This used to make sure that the user is authorized to do anything in this controller | |
# So take a look at the bottom for the authorization code it's very simple | |
# because of the dynamic user method | |
before :ensure_authorization | |
def index | |
# | |
end | |
def show(id) | |
# | |
end | |
def new | |
# | |
end | |
def edit(id) | |
# | |
end | |
def create(user) | |
# | |
end | |
def update(id, user, userinfo) | |
# | |
end | |
def destroy(id) | |
# | |
end | |
# only allow "user admins" or "super" users to use this controller | |
def ensure_authorization | |
throw(:halt, redirect("/race/home", :message => {:error => "You are not authorized to access that page." })) unless session.user.is_a_user_manager_or_super? | |
end | |
end # Users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment