diff options
author | guntasgrewal <guntasgrewal@gmail.com> | 2014-04-06 20:09:54 -0400 |
---|---|---|
committer | guntasgrewal <guntasgrewal@gmail.com> | 2014-04-06 20:09:54 -0400 |
commit | f3c395c56a644d976aa9a5608300557600bc683e (patch) | |
tree | 83cb961a721fa8c50903c70bb08ae14dd15ca58c /app/models/user.rb | |
parent | 7575d8cc70a28b323db0486ed06ab8af33b1f21a (diff) | |
parent | f85943114dba527a1f87abb03229553472f57c0c (diff) |
solved merge conflicts
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 158 |
1 files changed, 105 insertions, 53 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 64dd7ed..0b77ab1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,57 +9,109 @@ class User < ActiveRecord::Base before_save { self.email = email.downcase } before_save { self.user_name = user_name } - def after_initialize - self.permissions = 0 - end + def self.permission_bits + return { + :create_tournament => (2**1), + :edit_tournament => (2**2), + :join_tournament => (2**3), + :delete_tournament => (2**4), - def can?(action) - return true - case action - when :create_tournament - return true - when :edit_tournament - return true - when :join_tournament - return true - when :delete_tournament + :create_game => (2**5), + :edit_game => (2**6), + :delete_game => (2**7), - when :create_game - when :edit_game - when :delete_game + :create_user => (2**8), + :edit_user => (2**9), + :delete_user => (2**10), - when :create_user - return false - when :edit_user - when :delete_user + :create_alert => (2**11), + :edit_alert => (2**12), + :delete_alert => (2**13), - when :create_alert - when :edit_alert - when :delete_alert + :create_pm => (2**14), + :edit_pm => (2**15), + :delete_pm => (2**16), - when :create_pm - when :edit_pm - when :delete_pm + :create_session => (2**17), + :delete_session => (2**18), - when :create_session - return false - when :delete_session + :edit_permissions => (2**19), + :edit_server => (2**20), + } + end - else + def can?(action) + bit = User.permission_bits[action] + if bit.nil? return false + else + return (self.permissions & bit != 0) + end + end + + def add_ability(action) + bit = User.permission_bits[action.to_sym] + unless bit.nil? + self.permissions |= bit + end + end + + def remove_ability(action) + bit = User.permission_bits[action.to_sym] + unless bit.nil? + self.permissions &= ~ bit + end + end + + + # A representation of the permission bits as a mock-array. + def abilities + @abilities ||= Abilities.new(self) + end + def abilities=(new) + new.each do |k,v| + if v == "0" + v = false + end + abilities[k] = v + end + end + + # A thin array-like wrapper around the permission bits to make it + # easy to modify them using a form. + class Abilities + def initialize(user) + @user = user + end + def [](ability) + return @user.can?(ability) + end + def []=(ability, val) + if val + @user.add_ability(ability) + else + @user.remove_ability(ability) + end + end + def keys + User.permission_bits.keys + end + def method_missing(name, *args) + if name.to_s.ends_with?('=') + self[name.to_s.sub(/=$/, '').to_sym] = args.first + else + return self[name.to_sym] + end end end - ## # VAILD_EMAIL is the regex used to validate a user given email. VALID_EMAIL_REG = /\A\S+@\S+\.\S+\z/i - ## # VALID_USER_NAME checks to make sure a user's user_name # is in the proper format. VALID_USER_NAME_REG = /\A[a-zA-Z0-9\-]+\z/ - ## # The following lines put a user account through a series of # validations in order to make sure all of their information # is in the proper format. @@ -78,7 +130,6 @@ class User < ActiveRecord::Base format: {with: VALID_USER_NAME_REG }, uniqueness: {case_sensitive: false }) - ## # Instead of adding password and password_confirmation # attributes, requiring the presence of a password, # requiring that pw and pw_com match, and add an authenticate @@ -88,26 +139,27 @@ class User < ActiveRecord::Base has_secure_password validates :password, length: { minimum: 6 } -end -class NilUser - def nil? - return true - end - def can?(action) - case action - when :create_user - return true - when :create_session + + class NilUser + def nil? return true - else - return false end - end - def method_missing(name, *args) - # Throw an error if User doesn't have this method - super unless User.new.respond_to?(name) - # User has this method -- return a blank value - # 'false' if the method ends with '?'; 'nil' otherwise. - name.ends_with?('?') ? false : nil + def can?(action) + case action + when :create_user + return true + when :create_session + return true + else + return false + end + end + def method_missing(name, *args) + # Throw an error if User doesn't have this method + super unless User.new.respond_to?(name) + # User has this method -- return a blank value + # 'false' if the method ends with '?'; 'nil' otherwise. + name.to_s.ends_with?('?') ? false : nil + end end end |