summaryrefslogtreecommitdiff
path: root/app/models/ability.rb
blob: f4068ca3bd2a381017c893745b9c67754777d7d7 (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
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

          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

          # SystemMessages
          #
          cannot [:destroy, :edit, :update], SystemMessage

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

          # 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
        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 :manage, CallForward, :phone_number_id => sip_account.phone_number_ids
            can :manage, Ringtone, :ringtoneable_type => 'PhoneNumber', :ringtoneable_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 the PhoneNumbers of his
          # own SipAccounts:
          #
          can :manage, CallForward, :phone_number_id => user.phone_number_ids

          # SystemMessages
          #
          can :read, SystemMessage, :user_id => user.id

          # 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
      end
    end

  end
end