profile_members_controller.rb
4.38 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
class ProfileMembersController < MyProfileController
protect 'manage_memberships', :profile
no_design_blocks
def index
@members = profile.members
@member_role = environment.roles.find_by_name('member')
end
def update_roles
@roles = params[:roles] ? environment.roles.find(params[:roles].select{|r|!r.to_i.zero?}) : []
@roles = @roles.select{|r| r.has_kind?('Profile') }
begin
@person = profile.members.find(params[:person])
rescue ActiveRecord::RecordNotFound
@person = nil
end
if !params[:confirmation] && @person && @person.is_last_admin_leaving?(profile, @roles)
redirect_to :action => :last_admin, :roles => params[:roles], :person => @person
else
if @person && @person.define_roles(@roles, profile)
session[:notice] = _('Roles successfuly updated')
else
session[:notice] = _('Couldn\'t change the roles')
end
if params[:confirmation]
redirect_to profile.url
else
redirect_to :action => :index
end
end
end
def last_admin
@person = params[:person]
@roles = params[:roles] || []
@members = profile.members.select {|member| !profile.admins.include?(member)}
@title = _('Current admins')
@collection = :profile_admins
@remove_action = {:action => 'remove_admin'}
end
def add_role
@person = Person.find(params[:person])
@role = environment.roles.find(params[:role])
if @profile.affiliate(@person, @role)
redirect_to :action => 'index'
else
@member = Person.find(params[:person])
@roles = environment.roles.find(:all).select{ |r| r.has_kind?('Profile') }
render :action => 'affiliate'
end
end
def remove_role
@association = RoleAssignment.find(:all, :conditions => {:id => params[:id], :target_id => profile.id})
if @association.destroy
session[:notice] = 'Member succefully unassociated'
else
session[:notice] = 'Failed to unassociate member'
end
render :layout => false
end
def change_role
@roles = Profile::Roles.organization_member_roles(environment.id)
begin
@member = profile.members.find(params[:id])
rescue ActiveRecord::RecordNotFound
@member = nil
end
if @member
@associations = @member.find_roles(@profile)
else
redirect_to :action => :index
end
end
def unassociate
member = Person.find(params[:id])
associations = member.find_roles(profile)
RoleAssignment.transaction do
if associations.map(&:destroy)
session[:notice] = _('Member succesfully unassociated')
else
session[:notice] = _('Failed to unassociate member')
end
end
render :layout => false
end
def add_members
end
def add_member
if profile.enterprise?
member = Person.find(params[:id])
member.define_roles(Profile::Roles.all_roles(environment), profile)
end
render :layout => false
end
def add_admin
@title = _('Current admins')
@collection = :profile_admins
if profile.community?
member = profile.members.find_by_identifier(params[:id])
profile.add_admin(member)
end
render :layout => false
end
def remove_admin
@title = _('Current admins')
@collection = :profile_admins
if profile.community?
member = profile.members.find_by_identifier(params[:id])
profile.remove_admin(member)
end
render :layout => false
end
def find_users
if !params[:query] || params[:query].length <= 2
@users_found = []
elsif params[:scope] == 'all_users'
@users_found = Person.find_by_contents(params[:query] + '*').select {|user| !profile.members.include?(user)}
@button_alt = _('Add member')
@add_action = {:action => 'add_member'}
elsif params[:scope] == 'new_admins'
@users_found = Person.find_by_contents(params[:query] + '*').select {|user| profile.members.include?(user) && !profile.admins.include?(user)}
@button_alt = _('Add member')
@add_action = {:action => 'add_admin'}
end
render :layout => false
end
def send_mail
@mailing = profile.mailings.build(params[:mailing])
if request.post?
@mailing.locale = locale
@mailing.person = user
if @mailing.save
session[:notice] = _('The e-mails are being sent')
redirect_to :action => 'index'
else
session[:notice] = _('Could not create the e-mail')
end
end
end
end