bsc_plugin.rb
4.05 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
class BscPlugin < Noosfero::Plugin
Bsc
def self.plugin_name
"Bsc"
end
def self.plugin_description
_("Adds the Bsc feature")
end
def admin_panel_links
[{:title => _('Create Bsc'), :url => {:controller => 'bsc_plugin_admin', :action => 'new'}},
{:title => _('Validate Enterprises'), :url => {:controller => 'bsc_plugin_admin', :action => 'validate_enterprises'}} ]
end
def control_panel_buttons
buttons = []
buttons << {:title => _("Manage associated enterprises"), :icon => 'bsc-enterprises', :url => {:controller => 'bsc_plugin_myprofile', :action => 'manage_associated_enterprises'}} if bsc?(context.profile)
buttons << {:title => _('Transfer ownership'), :icon => 'transfer-enterprise-ownership', :url => {:controller => 'bsc_plugin_myprofile', :action => 'transfer_ownership'}} if context.profile.enterprise?
buttons << {:title => _("Manage contracts"), :icon => '', :url => {:controller => 'bsc_plugin_myprofile', :action => 'manage_contracts'}} if bsc?(context.profile)
buttons
end
def manage_members_extra_buttons
{:title => _('Transfer ownership'), :icon => '', :url => {:controller => 'bsc_plugin_myprofile', :action => 'transfer_enterprises_management'}} if context.profile.enterprise?
end
def stylesheet?
true
end
def catalog_list_item_extras(product)
if bsc?(context.profile)
enterprise = product.enterprise
if is_member_of_any_bsc?(context.user)
lambda {link_to(enterprise.short_name, enterprise.url, :class => 'bsc-catalog-enterprise-link')}
else
lambda {enterprise.short_name}
end
end
end
def profile_controller_filters
if profile
special_enterprise = profile.enterprise? && !profile.validated && profile.bsc
is_member_of_any_bsc = is_member_of_any_bsc?(context.user)
block = lambda {
render_access_denied if special_enterprise && !is_member_of_any_bsc
}
[{ :type => 'before_filter', :method_name => 'bsc_access', :block => block }]
else
[]
end
end
def content_viewer_controller_filters
if profile
special_enterprise = profile.enterprise? && !profile.validated && profile.bsc
is_member_of_any_bsc = is_member_of_any_bsc?(context.user)
block = lambda {
render_access_denied if special_enterprise && !is_member_of_any_bsc
}
[{ :type => 'before_filter', :method_name => 'bsc_access', :block => block }]
else
[]
end
end
def profile_editor_controller_filters
if context.user
is_not_admin = !context.environment.admins.include?(context.user)
[{ :type => 'before_filter',
:method_name => 'bsc_destroy_access',
:options => {:only => :destroy_profile},
:block => lambda { render_access_denied if is_not_admin } }]
else
[]
end
end
def manage_products_controller_filters
if bsc?(profile)
[{ :type => 'before_filter',
:method_name => 'manage_products_bsc_destroy_access',
:options => {:only => :destroy},
:block => lambda { render_access_denied } }]
else
[]
end
end
def asset_product_properties(product)
properties = []
properties << { :name => _('Bsc'), :content => lambda { link_to(product.bsc.name, product.bsc.url) } } if product.bsc
if product.enterprise.validated || is_member_of_any_bsc?(context.user)
content = lambda { link_to_homepage(product.enterprise.name, product.enterprise.identifier) }
else
content = lambda { product.enterprise.name }
end
properties << { :name => c_('Supplier'), :content => content }
end
def profile_tabs
if bsc?(context.profile)
{ :title => _("Contact"),
:id => 'bsc-contact',
:content => lambda { render :partial => 'profile_tab' },
:start => true }
end
end
private
def bsc?(profile)
profile.kind_of?(BscPlugin::Bsc)
end
def is_member_of_any_bsc?(user)
BscPlugin::Bsc.all.any? { |bsc| bsc.members.include?(user) }
end
def profile
context.environment.profiles.find_by_identifier(context.params[:profile])
end
end