Guest User

Untitled

a guest
Jul 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2.  
  3. # Associations
  4. belongs_to :website
  5.  
  6. # Filters
  7. before_validation :confirm_website
  8. before_save :confirm_website
  9.  
  10. # Authentication Module
  11. devise :all, :except => [:confirmable]
  12.  
  13. # Attribute
  14. attr_accessible :email, :password, :firstname, :initials, :lastname
  15.  
  16. # External User Model Files
  17. concerned_with :class_methods, :instance_methods
  18.  
  19. def is_admin?
  20. true if role.eql?("admin")
  21. end
  22.  
  23. def is_user?
  24. true if role.eql?("user")
  25. end
  26.  
  27. def has_permission?(current_user, action)
  28. return true if current_user.eql?(self) if action.eql?(:edit)
  29. return true if current_user.eql?(self) unless action.eql?(:destroy)
  30. return true if current_user.is_admin? and action.eql?(:edit) unless self.is_admin?
  31. return true if current_user.is_admin? and action.eql?(:destroy) unless current_user.eql?(self) or self.is_admin?
  32.  
  33. raise "No Access"
  34. end
  35.  
  36. private
  37.  
  38. def confirm_website
  39. strip_domain
  40. self.email = "#{email}@#{website.url}"
  41. end
  42.  
  43. def strip_domain
  44. self.email = email.slice(0, email.index('@')) if self.email.include?('@')
  45. end
  46.  
  47. end
Add Comment
Please, Sign In to add comment