Module: MnoEnterprise::Concerns::Models::Ability

Extended by:
ActiveSupport::Concern
Included in:
Ability
Defined in:
lib/mno_enterprise/concerns/models/ability.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#admin_abilities(user) ⇒ Object

Abilities for admin user



143
144
145
146
147
# File 'lib/mno_enterprise/concerns/models/ability.rb', line 143

def admin_abilities(user)
  if user.admin_role == 'admin'
    can :manage_app_instances, MnoEnterprise::Organization
  end
end

#impac_abilities(user) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mno_enterprise/concerns/models/ability.rb', line 106

def impac_abilities(user)
  can :manage_impac, MnoEnterprise::Impac::Dashboard do |dhb|
    dhb.organizations.any? && dhb.organizations.all? do |org|
      !!user.role(org) && ['Super Admin', 'Admin'].include?(user.role(org))
    end
  end

  can :manage_dashboard, MnoEnterprise::Impac::Dashboard do |dashboard|
    if dashboard.owner_type == "Organization"
      # The current user is a member of the organization that owns the dashboard that has the kpi attached to
      owner = MnoEnterprise::Organization.find(dashboard.owner_id)
      owner && !!user.role(owner)
    elsif dashboard.owner_type == "User"
      # The current user is the owner of the dashboard that has the kpi attached to
      dashboard.owner_id == user.id
    else
      false
    end
  end

  can :manage_widget, MnoEnterprise::Impac::Widget do |widget|
    dashboard = widget.dashboard
    authorize! :manage_dashboard, dashboard
  end

  can :manage_kpi, MnoEnterprise::Impac::Kpi do |kpi|
    dashboard = kpi.dashboard
    authorize! :manage_dashboard, dashboard
  end

  can :manage_alert, MnoEnterprise::Impac::Alert do |alert|
    kpi = alert.kpi
    authorize! :manage_kpi, kpi
  end
end

#initialize(user) ⇒ Object

Instance methods



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mno_enterprise/concerns/models/ability.rb', line 22

def initialize(user)
  user ||= MnoEnterprise::User.new

  #===================================================
  # Organization
  #===================================================
  can :create, MnoEnterprise::Organization

  can :read, MnoEnterprise::Organization do |organization|
    !!user.role(organization)
  end

  can [:update, :destroy, :manage_billing], MnoEnterprise::Organization do |organization|
    user.role(organization) == 'Super Admin'
  end

  can [:upload,
       :purchase,
       :invite_member,
       :administrate,
       :manage_app_instances,
       :manage_teams], MnoEnterprise::Organization do |organization|
    ['Super Admin','Admin'].include? user.role(organization)
  end

  # To be updated
  can :sync_apps, MnoEnterprise::Organization do |organization|
    user.role(organization)
  end

  # To be updated
  can :check_apps_sync, MnoEnterprise::Organization do |organization|
    user.role(organization)
  end

  #===================================================
  # AppInstance
  #===================================================
  can :access, MnoEnterprise::AppInstance do |app_instance|
    !!user.role(app_instance.owner) && (
    ['Super Admin','Admin'].include?(user.role(app_instance.owner)) ||
        user.teams.empty? ||
        user.teams.map(&:app_instances).compact.flatten.map(&:id).include?(app_instance.id)
    )
  end

  #===================================================
  # Impac
  #===================================================
  impac_abilities(user)

  #===================================================
  # Admin abilities
  #===================================================
  admin_abilities(user)

  # Define abilities for the passed in user here. For example:
  #
  #   user ||= User.new # guest user (not logged in)
  #   if user.admin?
  #     can :manage, :all
  #   else
  #     can :read, :all
  #   end
  #
  # The first argument to `can` is the action you are giving the user
  # permission to do.
  # If you pass :manage it will apply to every action. Other common actions
  # here are :read, :create, :update and :destroy.
  #
  # The second argument is the resource the user can perform the action on.
  # If you pass :all it will apply to every resource. Otherwise pass a Ruby
  # class of the resource.
  #
  # The third argument is an optional hash of conditions to further filter the
  # objects.
  # For example, here the user can only update published articles.
  #
  #   can :update, Article, :published => true
  #
  # See the wiki for details:
  # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
end