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_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
80 81 82 83 |
# File 'app/models/refinery/user.rb', line 80 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
65 66 67 |
# File 'app/models/refinery/user.rb', line 65 def plugins.collect(&:name) | ::Refinery::Plugins.always_allowed.names end |
#can_delete?(user_to_delete = self) ⇒ Boolean
69 70 71 72 73 74 |
# File 'app/models/refinery/user.rb', line 69 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
76 77 78 |
# File 'app/models/refinery/user.rb', line 76 def can_edit?(user_to_edit = self) user_to_edit.persisted? && (user_to_edit == self || self.has_role?(:superuser)) end |
#create_first ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/refinery/user.rb', line 90 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
85 86 87 88 |
# File 'app/models/refinery/user.rb', line 85 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/refinery/user.rb', line 38 def plugins=(plugin_names) return unless persisted? plugin_names = plugin_names.dup plugin_names.reject! { |plugin_name| !plugin_name.is_a?(String) } if plugins.empty? plugin_names.each_with_index do |plugin_name, index| plugins.create(:name => plugin_name, :position => index) end else assigned_plugins = plugins.all assigned_plugins.each do |assigned_plugin| if plugin_names.include?(assigned_plugin.name) plugin_names.delete(assigned_plugin.name) else assigned_plugin.destroy end end plugin_names.each do |plugin_name| plugins.create(:name => plugin_name, :position => plugins.select(:position).map{|p| p.position.to_i}.max + 1) end end end |
#to_s ⇒ Object
106 107 108 |
# File 'app/models/refinery/user.rb', line 106 def to_s username.to_s end |