organization.rb
7.22 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Represents any organization of the system
class Organization < Profile
attr_accessible :moderated_articles, :foundation_year, :contact_person, :acronym, :legal_form, :economic_activity, :management_information, :cnpj, :display_name, :enable_contact_us
attr_accessible :requires_email
settings_items :requires_email, type: :boolean
alias_method :requires_email?, :requires_email
SEARCH_FILTERS = {
:order => %w[more_recent more_popular more_active],
:display => %w[compact]
}
# An Organization is considered visible to a given person if one of the
# following conditions are met:
# 1) The user is an environment administrator.
# 2) The user is an administrator of the organization.
# 3) The user is a member of the organization and the organization is
# visible.
# 4) The user is not a member of the organization but the organization is
# visible, public and enabled.
def self.listed_for_person(person)
joins('LEFT JOIN "role_assignments" ON ("role_assignments"."resource_id" = "profiles"."id"
AND "role_assignments"."resource_type" = \'Profile\') OR (
"role_assignments"."resource_id" = "profiles"."environment_id" AND
"role_assignments"."resource_type" = \'Environment\' )')
.joins('LEFT JOIN "roles" ON "role_assignments"."role_id" = "roles"."id"')
.where(
['( (roles.key = ? OR roles.key = ?) AND role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? )
OR
( ( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR
( profiles.enabled = ? ) ) AND
( profiles.visible = ? ) )',
'profile_admin', 'environment_administrator', Profile.name, person.id,
Profile.name, person.id, true, true]
).uniq
end
def self.visible_for_person(person)
listed_for_person(person).where(
['( (roles.key = ? OR roles.key = ?) AND role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? )
OR
( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR
( profiles.enabled = ? AND profiles.public_profile = ? ) )',
'profile_admin', 'environment_administrator', Profile.name, person.id,
Profile.name, person.id, true, true]
)
end
settings_items :closed, :type => :boolean, :default => false
def closed?
closed
end
before_save do |organization|
organization.closed = true if !organization.public_profile?
end
settings_items :moderated_articles, :type => :boolean, :default => false
def moderated_articles?
moderated_articles
end
has_one :validation_info
has_many :validations, :class_name => 'CreateEnterprise', :foreign_key => :target_id
has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source'
has_many :custom_roles, :class_name => 'Role', :foreign_key => :profile_id
scope :more_popular, -> { order 'members_count DESC' }
validate :presence_of_required_fieds, :unless => :is_template
def self.notify_activity tracked_action
Delayed::Job.enqueue NotifyActivityToProfilesJob.new(tracked_action.id)
end
def presence_of_required_fieds
self.required_fields.each do |field|
if self.send(field).blank?
self.errors.add_on_blank(field)
end
end
end
def validation_methodology
self.validation_info ? self.validation_info.validation_methodology : nil
end
def validation_restrictions
self.validation_info ? self.validation_info.restrictions : nil
end
def pending_validations
validations.pending
end
def find_pending_validation(code)
validations.pending.where(code: code).first
end
def processed_validations
validations.finished
end
def find_processed_validation(code)
validations.finished.where(code: code).first
end
def is_validation_entity?
!self.validation_info.nil?
end
FIELDS = %w[
display_name
description
contact_person
contact_email
contact_phone
legal_form
economic_activity
management_information
address
zip_code
city
state
country
tag_list
template_id
district
address_line2
address_reference
]
def self.fields
FIELDS
end
def required_fields
[]
end
def active_fields
[]
end
def signup_fields
[]
end
N_('Display name'); N_('Description'); N_('Contact person'); N_('Contact email'); N_('Acronym'); N_('Foundation year'); N_('Legal form'); N_('Economic activity'); N_('Management information'); N_('Tag list'); N_('District'); N_('Address reference')
settings_items :display_name, :description, :contact_person, :contact_email, :acronym, :foundation_year, :legal_form, :economic_activity, :management_information, :district, :address_line2, :address_reference
settings_items :zip_code, :city, :state, :country
validates_numericality_of :foundation_year, only_integer: true, if: -> o { o.foundation_year.present? }
validates_format_of :contact_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |org| !org.contact_email.blank? })
validates_as_cnpj :cnpj
xss_terminate :only => [ :acronym, :contact_person, :contact_email, :legal_form, :economic_activity, :management_information ], :on => 'validation'
# Yes, organizations have members.
#
# Returns <tt>true</tt>.
def has_members?
true
end
def default_set_of_blocks
links = [
{:name => _("Community's profile"), :address => '/profile/{profile}', :icon => 'ok'},
{:name => _('Invite Friends'), :address => '/profile/{profile}/invite/friends', :icon => 'send'},
{:name => _('Agenda'), :address => '/profile/{profile}/events', :icon => 'event'},
{:name => _('Image gallery'), :address => '/{profile}/gallery', :icon => 'photos'},
{:name => _('Blog'), :address => '/{profile}/blog', :icon => 'edit'},
]
[
[MainBlock.new],
[ProfileImageBlock.new, LinkListBlock.new(:links => links)],
[RecentDocumentsBlock.new]
]
end
def default_set_of_articles
[
Blog.new(:name => _('Blog')),
Gallery.new(:name => _('Gallery')),
]
end
def short_name chars = 40
s = self.display_name
s = super(chars) if s.blank?
s
end
def notification_emails
emails = [contact_email].select(&:present?) + admins.map(&:email)
if emails.empty?
emails << environment.contact_email
end
emails
end
def already_request_membership?(person)
self.tasks.pending.where(type: 'AddMember', requestor_id: person.id).first
end
def jid(options = {})
super({:domain => "conference.#{environment.default_hostname}"}.merge(options))
end
def receives_scrap_notification?
false
end
def members_to_json
members.map { |member| {:id => member.id, :name => member.name} }.to_json
end
def members_by_role_to_json(role)
members_by_role(role).map { |member| {:id => member.id, :name => member.name} }.to_json
end
def disable
self.visible = false
save!
end
def allow_invitation_from?(person)
(followed_by?(person) && self.allow_members_to_invite) || person.has_permission?('invite-members', self)
end
def is_admin?(user)
self.admins.where(:id => user.id).exists?
end
def display_private_info_to?(user)
(public_profile && visible && !secret) || super
end
end