Skip to content

Commit

Permalink
Adding more code that the Refinery Authentication system requires. Na…
Browse files Browse the repository at this point in the history
…mely FriendlyId related code. Also pointing username to login. We are creating a Refinery::User model that inherits from Spree::User.
  • Loading branch information
maleko committed May 16, 2012
1 parent 6c08cc2 commit daf7d37
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 57 deletions.
5 changes: 5 additions & 0 deletions app/models/refinery/user.rb
@@ -0,0 +1,5 @@
module Refinery
class User < Spree::User

end
end
149 changes: 95 additions & 54 deletions app/models/spree/user_decorator.rb
@@ -1,56 +1,97 @@
Spree::User.class_eval do
extend FriendlyId
# For Refinery
accepts_nested_attributes_for :roles
attr_accessible :roles_attributes, :roles
has_many :plugins, :class_name => "Refinery::UserPlugin", :order => "position ASC", :dependent => :destroy
accepts_nested_attributes_for :plugins
attr_accessible :plugins_attributes, :plugins
friendly_id :login

# For Refinery
accepts_nested_attributes_for :roles
attr_accessible :roles_attributes, :roles
has_many :plugins, :class_name => "Refinery::UserPlugin", :order => "position ASC", :dependent => :destroy
accepts_nested_attributes_for :plugins
attr_accessible :plugins_attributes, :plugins

scope :refinery_admin, lambda { includes(:roles).where("#{roles_table_name}.name" => "Refinery") }


has_many :plugins, :class_name => "::Refinery::UserPlugin", :order => "position ASC", :dependent => :destroy

def plugins=(plugin_names)
if persisted? # don't add plugins when the user_id is nil.
::Refinery::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

def authorized_plugins
plugins.collect { |p| p.name } | ::Refinery::Plugins.always_allowed.names
end

def add_role(title)
raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(Spree::Role)
roles << Spree::Role[title] unless has_role?(title)
end

def has_role?(title)
raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(Spree::Role)
roles.any?{|r| r.name == title.to_s.camelize || r.name = title.to_s}
end

def can_edit?(user_to_edit = self)
user_to_edit.persisted? && (
user_to_edit == self ||
self.has_role?(:superuser)
)
end

def can_delete?(user_to_delete = self)
user_to_delete.persisted? and
id != user_to_delete.id and
!user_to_delete.has_role?(:superuser) and
Spree::Role[:refinery].users.count > 1
end

def title
self.name
end
end
scope :refinery_admin, lambda { includes(:roles).where("#{roles_table_name}.name" => "Refinery") }

has_many :plugins, :class_name => "::Refinery::UserPlugin", :order => "position ASC", :dependent => :destroy

# Find user by email or username.
# https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign_in-using-their-username-or-email-address
def self.find_for_database_authentication(conditions)
value = conditions[authentication_keys.first]
where(["login = :value OR email = :value", {:value => value}]).first
end

def plugins=(plugin_names)
if persisted? # don't add plugins when the user_id is nil.
::Refinery::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

def authorized_plugins
plugins.collect { |p| p.name } | ::Refinery::Plugins.always_allowed.names
end

def add_role(title)
raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(Spree::Role)
roles << Spree::Role[title] unless has_role?(title)
end

def has_role?(title)
raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(Spree::Role)
roles.any?{|r| r.name == title.to_s.camelize || r.name = title.to_s}
end

def can_edit?(user_to_edit = self)
user_to_edit.persisted? && (
user_to_edit == self ||
self.has_role?(:superuser)
)
end

def can_delete?(user_to_delete = self)
user_to_delete.persisted? and
id != user_to_delete.id and
!user_to_delete.has_role?(:superuser) and
Spree::Role[:refinery].users.count > 1
end

def title
self.name
end

def create_first
if valid?
# first we need to save user
save
# add refinery role
add_role(:refinery)
# add superuser role
add_role(:superuser) if ::Refinery::Role[:refinery].users.count == 1
# add plugins
self.plugins = Refinery::Plugins.registered.in_menu.names
end

# return true/false based on validations
valid?
end

def username
login
end

def username=(value)
login = value
end

def to_s
login.to_s
end

def to_param
to_s.parameterize
end


end
2 changes: 1 addition & 1 deletion lib/refinery/authenticated_system.rb
Expand Up @@ -34,7 +34,7 @@ def refinery_user?

### PATCH FOR SPREE AUTH *and* REFINERY AUTH
def current_refinery_user
current_user
current_refinery_user ||= Refinery::User.find(current_user)
end

def refinery_user_signed_in?
Expand Down
6 changes: 4 additions & 2 deletions spree_refinery_auth.gemspec
Expand Up @@ -20,9 +20,11 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.requirements << 'none'

s.add_dependency 'spree_core', '1.1.0'
s.add_dependency 'spree_core', '1.1.0'
s.add_dependency 'friendly_id', '~> 4.0.1'

s.add_development_dependency 'capybara', '1.0.1'

s.add_development_dependency 'capybara', '1.0.1'
s.add_development_dependency 'factory_girl'
s.add_development_dependency 'ffaker'
s.add_development_dependency 'rspec-rails', '~> 2.7'
Expand Down

0 comments on commit daf7d37

Please sign in to comment.