ryanb / cancan

Authorization Gem for Ruby on Rails.
MIT License
6.26k stars 782 forks source link

Not assigning user after I added CanCan #881

Open Hatcher19 opened 11 years ago

Hatcher19 commented 11 years ago

I have an activeadmin app that I have just installed cancan on. I have a model for orders and admin_user. Before the installation of cancan, this was working. When I create an order with a user that has a "broker" and "production" role, it works fine. When I do the same for users with the roles "admin", and "sales", it does not assign a user to the order.

here is my ability model

class Ability
  include CanCan::Ability

  def initialize(user)

    return if user.nil? #non logged in user can use this.

     if user.admin?
        can :manage, :all
    end

    if user.sales?
        can [:index, :create, :read, :update, :new, :edit], [Order, Customer], :admin_user_id => user.id
        cannot [:create, :destroy], [OrderCategory, OrderType, OrderStatus, OrderPriority, PrintLocation, AdminUser]
    end

    if user.broker?
        can [:index, :create, :read, :update, :new, :edit], [Order, Customer], :admin_user_id => user.id
        can :read, [OrderCategory, OrderType, OrderStatus, OrderPriority, PrintLocation, AdminUser]
        cannot :index, [OrderCategory, OrderType, OrderStatus, OrderPriority, PrintLocation, AdminUser]
        cannot :destroy, :all
    end

    if user.art?
        cannot :create, :all
        can :read, :all
        can :update, Order
        cannot :destroy, :all
    end

    if user.shipping?
        can :read, :all
        can :update, Order
        cannot :destroy, :all
    end

    if user.production?
        can [:create, :update], [Order, Customer]
        can :read, :all
    end
end

end

here is the relavant code in my order controller

ActiveAdmin.register Order, :sort_order => "end_date_asc" do
controller.authorize_resource :except => :index

scope_to :current_manager, :association_method => :orders

menu :label => "Orders"

filter :name, label: "Order Name"
filter :admin_user, :collection => proc { AdminUser.all.map{|u| [u.last_name, u.id] } }
filter :order_category, label: "Category"
filter :order_type, label: "Type"
filter :order_status, label: "Status"
filter :order_priority, label: "Priority"
filter :customer, label: "Customer"
    filter :start_date, label: "Start Date"
    filter :end_date, label: "Due Date"
    filter :id, label: "Order ID#"

  controller do
    #https://github.com/gregbell/active_admin/issues/732
    #scope index if not admin or sales.
   def current_manager
         if current_user.broker?
       current_user
     end
    end
   end
xhoy commented 10 years ago

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013. Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!