Sha256: d15f211a54fd519deb9b1842662cc7832a67ce00a7d4149f4fa92a8f221a247a

Contents?: true

Size: 1.84 KB

Versions: 10

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
# See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities

class Ability
  include CanCan::Ability

  def initialize(user)
    # handle signup
    can(:create, User) if User.can_signup?

    if user.present?
      entities = [Account, Campaign, Contact, Lead, Opportunity]

      # User
      can :manage, User, id: user.id # can do any action on themselves

      # Tasks
      can :create, Task
      can :manage, Task, user: user.id
      can :manage, Task, assigned_to: user.id
      can :manage, Task, completed_by: user.id

      # Entities
      can :manage, entities, access: 'Public'
      can :manage, entities + [Task], user_id: user.id
      can :manage, entities + [Task], assigned_to: user.id

      #
      # Due to an obscure bug (see https://github.com/ryanb/cancan/issues/213)
      # we must switch on user.admin? here to avoid the nil constraints which
      # activate the issue referred to above.
      #
      if user.admin?
        can :manage, :all
      else
        # Group or User permissions
        t = Permission.arel_table
        scope = t[:user_id].eq(user.id)

        if (group_ids = user.group_ids).any?
          scope = scope.or(t[:group_id].eq_any(group_ids))
        end

        permissions = Permission.select(:asset_type, :asset_id).where(scope).where(asset_type: entities.map { |k| k.name.to_s })
        permissions.each do |p|
          can :manage, p.asset_type.constantize, id: p.asset_id
        end
      end

    end
  end

  ActiveSupport.run_load_hooks(:fat_free_crm_ability, self)
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fat_free_crm-0.22.1 app/models/users/ability.rb
fat_free_crm-0.22.0 app/models/users/ability.rb
fat_free_crm-0.21.0 app/models/users/ability.rb
fat_free_crm-0.20.1 app/models/users/ability.rb
fat_free_crm-0.20.0 app/models/users/ability.rb
fat_free_crm-0.19.2 app/models/users/ability.rb
fat_free_crm-0.19.0 app/models/users/ability.rb
fat_free_crm-0.18.2 app/models/users/ability.rb
fat_free_crm-0.18.1 app/models/users/ability.rb
fat_free_crm-0.18.0 app/models/users/ability.rb