summaryrefslogtreecommitdiff
path: root/app/models/ability.rb
blob: fe6754785e0870e4499881d90d9263ceb0fea7cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
105
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class Ability
  include CanCan::Ability
  
  def initialize( user )
    # See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities
    if user && user.current_tenant != nil
      if GemeinschaftSetup.count == 1 && Tenant.count == 1 && User.count == 1 && UserGroup.count == 1
        # This is a new installation with a Master-Tenant and a Super-Admin.
        #
        can [:read, :create], Tenant
      else
        tenant = user.current_tenant

        if user.current_tenant.user_groups.where(:name => 'Admins').first \
          && user.current_tenant.user_groups.where(:name => 'Admins').first.users.include?(user)
          # ADMIN ABILITIES
          # With great power comes great responsibility!
          #
          can :manage, :all

          # Manufacturers and PhoneModels can not be changed
          #
          cannot [:create, :destroy, :edit, :update], Manufacturer
          cannot [:create, :destroy, :edit, :update], PhoneModel

          # Super-Tenant can not be destroyed or edited
          #
          cannot [:create, :destroy, :edit, :update], Tenant, :id => 1

          # Can't destroy any tenant
          #
          cannot :destroy, Tenant

          cannot :manage, PhoneBook

          # Phonebooks and PhoneBookEntries
          #
          can :manage, PhoneBook, :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id
          can :manage, PhoneBookEntry, :phone_book => { :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id }

          can :manage, PhoneBook, :phone_bookable_type => 'UserGroup', :phone_bookable_id => tenant.user_group_ids
          tenant.user_groups.each do |user_group|
            can :manage, PhoneBookEntry, :phone_book => { :id => user_group.phone_book_ids }
          end

          # Personal Phonebooks and PhoneBookEntries
          #
          can :manage, PhoneBook, :phone_bookable_type => 'User', :phone_bookable_id => user.id
          can :manage, PhoneBookEntry, :phone_book => { :phone_bookable_type => 'User', :phone_bookable_id => user.id }

          can :read, PhoneBook, :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id
          can :read, PhoneBookEntry, :phone_book => { :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id }

          can :read, PhoneBook, :phone_bookable_type => 'UserGroup', :phone_bookable_id => user.user_group_ids
          user.user_groups.each do |user_group|
            can :read, PhoneBookEntry, :phone_book => { :id => user_group.phone_book_ids }
          end

          # A FacDocument can't be changed
          #
          cannot [:edit, :update], FaxDocument

          # Backups can't be edited
          #
          cannot [:edit, :update], BackupJob

          # Can manage GsNodes
          #
          can :manage, GsNode

          # Can't phones/1/phone_sip_accounts/1/edit
          #
          cannot :edit, PhoneSipAccount

          # Dirty hack to disable PhoneNumberRange in the GUI
          #
          if GsParameter.get('STRICT_INTERNAL_EXTENSION_HANDLING') == false
            cannot :manage, PhoneNumberRange
          end

          # GsParameter and GuiFunction can't be created or deleted via the GUI
          #
          cannot [:create, :destroy], GsParameter
          cannot [:create, :destroy], GuiFunction

          # An admin can not destroy his/her account
          #
          cannot [:destroy], User, :id => user.id

          # SIM cards
          #
          cannot [:edit, :update], SimCard

          # Restore is only possible on a new system.
          #
          cannot :manage, RestoreJob

        else
          # Any user can do the following stuff.
          #

          # Own Tenant and own User
          #
          can :read, Tenant, :id => user.current_tenant.id
          can [ :read, :edit, :update ], User, :id => user.id

          # Destroy his own avatar
          #
          can :destroy_avatar, User, :id => user.id

          # Phonebooks and PhoneBookEntries
          #
          cannot :manage, PhoneBook

          can :manage, PhoneBook, :phone_bookable_type => 'User', :phone_bookable_id => user.id
          can :manage, PhoneBookEntry, :phone_book => { :phone_bookable_type => 'User', :phone_bookable_id => user.id }
          can :manage, PhoneNumber, :phone_numberable_type => 'PhoneBookEntry', :phone_numberable_id => user.phone_books.map{ |phone_book| phone_book.phone_book_entry_ids}.flatten

          can :read, PhoneBook, :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id
          can :read, PhoneBookEntry, :phone_book => { :phone_bookable_type => 'Tenant', :phone_bookable_id => tenant.id }

          can :read, PhoneBook, :phone_bookable_type => 'UserGroup', :phone_bookable_id => user.user_group_ids
          user.user_groups.each do |user_group|
            can :read, PhoneBookEntry, :phone_book => { :id => user_group.phone_book_ids }
          end

          # UserGroups
          #
          can :read, UserGroupMembership, :user_id => user.id
          can :read, UserGroup, :users => { :user_group_memberships => { :user_id => user.id }}

          # SipAccounts and Phones
          #
          can :read, SipAccount, :sip_accountable_type => 'User', :sip_accountable_id => user.id
          user.sip_accounts.each do |sip_account|
            can :read, PhoneNumber, :id => sip_account.phone_number_ids
            can [:read, :destroy, :call] , CallHistory, :id => sip_account.call_history_ids
          end
          can :read, Phone, :phoneable_type => 'User', :phoneable_id => user.id

          # Softkeys
          #
          can :manage, Softkey, :sip_account => { :id => user.sip_account_ids }

          # Fax
          #
          can :read, FaxAccount, :fax_accountable_type => 'User', :fax_accountable_id => user.id
          user.fax_accounts.each do |fax_account|
            can :read, PhoneNumber, :id => fax_account.phone_number_ids
            can [:read, :create, :delete], FaxDocument, :fax_account_id => fax_account.id
          end 

          # Conferences
          #
          can [ :read, :edit, :update, :destroy ], Conference, :id => user.conference_ids
          user.conferences.each do |conference|
            can :read, PhoneNumber, :id => conference.phone_number_ids
            can :manage, ConferenceInvitee, :conference_id => conference.id
          end

          # User can manage CallForwards of its SipAccount and PhoneNumbers
          #
          can :manage, CallForward, :call_forwardable_type => 'PhoneNumber', :call_forwardable_id => user.phone_number_ids
          can :manage, CallForward, :call_forwardable_type => 'SipAccount',  :call_forwardable_id => user.sip_account_ids
          can :create, CallForward

          # User can manage Ringtones of its SipAccount and PhoneNumbers
          #
          can :manage, Ringtone, :ringtoneable_type => 'PhoneNumber', :ringtoneable_id => user.phone_number_ids
          can :manage, Ringtone, :ringtoneable_type => 'SipAccount',  :ringtoneable_id => user.sip_account_ids
          can :create, Ringtone

          # User can read and toggle status of ACD agents
          #
          can :read, AcdAgent, :destination_type => 'SipAccount',  :destination_id => user.sip_account_ids
          can :toggle, AcdAgent, :destination_type => 'SipAccount',  :destination_id => user.sip_account_ids

          # SoftkeyFunctions
          #
          can :read, SoftkeyFunction

          # Voicemail
          #
          can :manage, VoicemailMessage
          can :manage, VoicemailSetting
        end
      end
    else
      if GemeinschaftSetup.count == 0 && Tenant.count == 0 && User.count == 0
        # This is a fresh system.
        #
        can :create, GemeinschaftSetup
        can :manage, SipDomain
        can [:create, :new, :show, :index], RestoreJob
      end
    end

  end
end