Class: Refinery::User
- Inherits:
-
Core::BaseModel
- Object
- Core::BaseModel
- Refinery::User
- Extended by:
- FriendlyId
- Defined in:
- app/models/refinery/user.rb
Instance Attribute Summary collapse
-
#login ⇒ Object
Setup accessible (or protected) attributes for your model :login is a virtual attribute for authenticating by either username or email This is in addition to a real persisted field like ‘username’.
Class Method Summary collapse
-
.find_for_database_authentication(conditions) ⇒ Object
Find user by email or username.
Instance Method Summary collapse
- #add_role(title) ⇒ Object
- #authorized_plugins ⇒ Object
- #can_delete?(user_to_delete = self) ⇒ Boolean
- #can_edit?(user_to_edit = self) ⇒ Boolean
- #create_first ⇒ Object
- #has_role?(title) ⇒ Boolean
- #plugins=(plugin_names) ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#login ⇒ Object
Setup accessible (or protected) attributes for your model :login is a virtual attribute for authenticating by either username or email This is in addition to a real persisted field like ‘username’
23 24 25 |
# File 'app/models/refinery/user.rb', line 23 def login @login end |
Class Method Details
.find_for_database_authentication(conditions) ⇒ Object
Find user by email or username. github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address
32 33 34 35 |
# File 'app/models/refinery/user.rb', line 32 def find_for_database_authentication(conditions) value = conditions[authentication_keys.first] where(["username = :value OR email = :value", { :value => value }]).first end |
Instance Method Details
#add_role(title) ⇒ Object
66 67 68 69 |
# File 'app/models/refinery/user.rb', line 66 def add_role(title) raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(::Refinery::Role) roles << ::Refinery::Role[title] unless has_role?(title) end |
#authorized_plugins ⇒ Object
48 49 50 |
# File 'app/models/refinery/user.rb', line 48 def plugins.collect(&:name) | ::Refinery::Plugins.always_allowed.names end |
#can_delete?(user_to_delete = self) ⇒ Boolean
52 53 54 55 56 57 |
# File 'app/models/refinery/user.rb', line 52 def can_delete?(user_to_delete = self) user_to_delete.persisted? && !user_to_delete.has_role?(:superuser) && ::Refinery::Role[:refinery].users.any? && id != user_to_delete.id end |
#can_edit?(user_to_edit = self) ⇒ Boolean
59 60 61 62 63 64 |
# File 'app/models/refinery/user.rb', line 59 def can_edit?(user_to_edit = self) user_to_edit.persisted? && ( user_to_edit == self || self.has_role?(:superuser) ) end |
#create_first ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/refinery/user.rb', line 76 def create_first if valid? # first we need to save user save # add refinery role add_role(:refinery) # add superuser role if there are no other users add_role(:superuser) if ::Refinery::Role[:refinery].users.count == 1 # add plugins self.plugins = Refinery::Plugins.registered..names end # return true/false based on validations valid? end |
#has_role?(title) ⇒ Boolean
71 72 73 74 |
# File 'app/models/refinery/user.rb', line 71 def has_role?(title) raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(::Refinery::Role) roles.any?{|r| r.title == title.to_s.camelize} end |
#plugins=(plugin_names) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'app/models/refinery/user.rb', line 38 def plugins=(plugin_names) if persisted? # don't add plugins when the user_id is nil. UserPlugin.delete_all(:user_id => id) plugin_names.each_with_index do |plugin_name, index| plugins.create(:name => plugin_name, :position => index) if plugin_name.is_a?(String) end end end |
#to_param ⇒ Object
96 97 98 |
# File 'app/models/refinery/user.rb', line 96 def to_param to_s.parameterize end |
#to_s ⇒ Object
92 93 94 |
# File 'app/models/refinery/user.rb', line 92 def to_s username.to_s end |