1 | package LATMOS::Accounts::Web::Model::Accounts; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use FindBin; |
---|
6 | use lib "$FindBin::Bin/../../LATMOS-Accounts/lib"; |
---|
7 | use base 'Catalyst::Model'; |
---|
8 | use base 'LATMOS::Accounts'; |
---|
9 | use LATMOS::Accounts::Log; |
---|
10 | use utf8; |
---|
11 | |
---|
12 | la_set_log( |
---|
13 | syslog => [], |
---|
14 | console => LA_WARNING, |
---|
15 | ); |
---|
16 | |
---|
17 | =head1 NAME |
---|
18 | |
---|
19 | LATMOS::Accounts::Web::Model::Accounts - Catalyst Model |
---|
20 | |
---|
21 | =head1 DESCRIPTION |
---|
22 | |
---|
23 | Catalyst Model. |
---|
24 | |
---|
25 | =cut |
---|
26 | |
---|
27 | =head1 FUNCTIONS |
---|
28 | |
---|
29 | =cut |
---|
30 | |
---|
31 | sub new { |
---|
32 | my ($class) = @_; |
---|
33 | bless(LATMOS::Accounts |
---|
34 | ->new(LATMOS::Accounts::Web->config->{config}), |
---|
35 | $class); |
---|
36 | } |
---|
37 | |
---|
38 | sub ACCEPT_CONTEXT { |
---|
39 | my ($self, $c, $form, $object, $base) = @_; |
---|
40 | $self->{c} = $c; |
---|
41 | $self; |
---|
42 | } |
---|
43 | |
---|
44 | sub accounts { |
---|
45 | my ($self) = @_; |
---|
46 | $self; |
---|
47 | } |
---|
48 | |
---|
49 | sub db { |
---|
50 | my ($self) = @_; |
---|
51 | $self->{_default_base} and return $self->{_default_base}; |
---|
52 | $self->{_default_base} = $self->base; |
---|
53 | $self->{_default_base}->SetCallBack( |
---|
54 | 'commit', |
---|
55 | sub { $self->{c}->stash->{DBNeedSync} = 1; } |
---|
56 | ); |
---|
57 | $self->{_default_base}->load or return; |
---|
58 | $self->{_default_base}->wexported(1); |
---|
59 | $self->{_default_base} |
---|
60 | } |
---|
61 | |
---|
62 | =head2 obj_to_label($ref) |
---|
63 | |
---|
64 | Return a proper label to display for each object |
---|
65 | |
---|
66 | =cut |
---|
67 | |
---|
68 | sub obj_to_label { |
---|
69 | my ($self, $ref) = @_; |
---|
70 | my $uri_part = { |
---|
71 | user => 'Utilisateurs', |
---|
72 | group => 'Groupes', |
---|
73 | nethost => 'HÎtes réseaux', |
---|
74 | netzone => 'Zones réseau', |
---|
75 | site => 'Sites', |
---|
76 | aliases => 'Alias', |
---|
77 | service => 'Services', |
---|
78 | address => 'Adresse', |
---|
79 | employment => 'Statut', |
---|
80 | support => 'Support', |
---|
81 | }->{$ref}; |
---|
82 | } |
---|
83 | |
---|
84 | sub obj_to_uri { |
---|
85 | my ($self, $ref, $id) = @_; |
---|
86 | my $uri_part = { |
---|
87 | user => 'users', |
---|
88 | group => 'groups', |
---|
89 | nethost => 'nethosts', |
---|
90 | netzone => 'netzones', |
---|
91 | site => 'sites', |
---|
92 | aliases => 'aliases', |
---|
93 | service => 'services', |
---|
94 | support => 'support', |
---|
95 | }->{$ref} || $ref; |
---|
96 | |
---|
97 | return $self->{c}->uri_for('/', $uri_part, ($id ? $id : ())); |
---|
98 | } |
---|
99 | |
---|
100 | sub object_prev_next { |
---|
101 | my ($self, $otype, $id) = @_; |
---|
102 | |
---|
103 | my @list = $self->db->list_objects($otype); |
---|
104 | my $prev; |
---|
105 | while (@list && ($list[0] || '') ne $id) { |
---|
106 | $prev = shift(@list); |
---|
107 | } |
---|
108 | return([ $prev, $list[1] ]); |
---|
109 | } |
---|
110 | |
---|
111 | sub object_navigate { |
---|
112 | my ($self, $otype, $id, @filter) = @_; |
---|
113 | |
---|
114 | my @list = $self->db->search_objects($otype, @filter); |
---|
115 | |
---|
116 | my $i = 0; |
---|
117 | for($i = 0; $i <= $#list; $i++) { |
---|
118 | $list[$i] eq $id and last; |
---|
119 | } |
---|
120 | |
---|
121 | my %ptr = (); |
---|
122 | $ptr{oprev} = $list[$i-1] if ($i > 0); |
---|
123 | $ptr{'onext'} = $list[$i+1] if ($i < $#list); |
---|
124 | $ptr{ofirst} = $list[0] if ($i > 1); |
---|
125 | $ptr{'olast'} = $list[$#list] if ($i < $#list -1); |
---|
126 | $ptr{list} = \@list; |
---|
127 | |
---|
128 | return \%ptr; |
---|
129 | } |
---|
130 | |
---|
131 | sub CanCreateObjects { |
---|
132 | my ($self, @otype) = @_; |
---|
133 | |
---|
134 | my $db = $self->db; |
---|
135 | |
---|
136 | if (!@otype) { |
---|
137 | @otype = $db->list_supported_objects; |
---|
138 | } |
---|
139 | |
---|
140 | foreach (@otype) { |
---|
141 | if (!$db->check_acl($_, '@CREATE', 'w')) { |
---|
142 | return; |
---|
143 | } |
---|
144 | } |
---|
145 | return 1; |
---|
146 | } |
---|
147 | |
---|
148 | # Such function must not be here, but in LATMOS::Accounts |
---|
149 | # But code does not allow this at time |
---|
150 | sub list_unowned_aliases { |
---|
151 | my ($self, $filter) = @_; |
---|
152 | my $db = $self->db; |
---|
153 | my $sth = $db->db->prepare_cached(q{ |
---|
154 | select name, forward from aliases where |
---|
155 | name not in (select name from "user") |
---|
156 | and |
---|
157 | forward not in (select array[name] from "user") |
---|
158 | } . |
---|
159 | ($filter |
---|
160 | ? q{ |
---|
161 | and (lower(name) ILIKE $1 or |
---|
162 | lower(array_to_string(forward, ',')) ILIKE $1) |
---|
163 | } |
---|
164 | : '') |
---|
165 | ); |
---|
166 | |
---|
167 | $sth->execute($filter ? ('%' . $filter . '%') : ()); |
---|
168 | my %aliases; |
---|
169 | while (my $res = $sth->fetchrow_hashref) { |
---|
170 | $aliases{$res->{name}} = $res->{forward}; |
---|
171 | } |
---|
172 | return \%aliases |
---|
173 | } |
---|
174 | |
---|
175 | sub sync_access { |
---|
176 | my ($self) = @_; |
---|
177 | $self->SUPER::sync_access |
---|
178 | } |
---|
179 | |
---|
180 | sub sync { |
---|
181 | my ($self) = @_; |
---|
182 | $self->default_synchro() |
---|
183 | } |
---|
184 | |
---|
185 | sub ChangeUserPassword { |
---|
186 | my ($self, $username, $password, $force) = @_; |
---|
187 | |
---|
188 | my $base = $self->db; |
---|
189 | my $user = $base->get_object('user', $username) or do { |
---|
190 | return $self->{c}->localize('Cannot get user object [_1]', $username); |
---|
191 | }; |
---|
192 | |
---|
193 | my $sync = $self->sync_access; |
---|
194 | |
---|
195 | # This is widelly burk: |
---|
196 | # synchaccess do not manage connect() at time, opening a |
---|
197 | # backdoor then :\ |
---|
198 | foreach ($sync->bases) { $_->{_user} = $base->{_user} } |
---|
199 | |
---|
200 | my $suser = $sync->get_object_ifexists('user', $username); |
---|
201 | my $msg = $user->check_password($password); |
---|
202 | if ($msg ne 'ok' && !$force) { |
---|
203 | return $msg . ', ' . $self->{c}->localize('Unchanged password'); |
---|
204 | } elsif ($suser && $suser->_set_password($password)) { |
---|
205 | $sync->commit; |
---|
206 | return; |
---|
207 | } else { |
---|
208 | return $self->{c}->localize('Error while changing password'); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | sub call_batch_sync { |
---|
213 | my ($self) = @_; |
---|
214 | |
---|
215 | $self->{c}->log->debug('Running call_batch_sync()'); |
---|
216 | |
---|
217 | if (my $command = $self->{c}->config->{callSyncCmd}) { |
---|
218 | if (system($command) != 0) { |
---|
219 | $self->{c}->log->warn("Error while running callSyncCmd: $!"); |
---|
220 | } |
---|
221 | } else { |
---|
222 | $self->SUPER::call_batch_sync; |
---|
223 | } |
---|
224 | |
---|
225 | } |
---|
226 | |
---|
227 | =head1 AUTHOR |
---|
228 | |
---|
229 | Thauvin Olivier |
---|
230 | |
---|
231 | =head1 LICENSE |
---|
232 | |
---|
233 | This library is free software, you can redistribute it and/or modify |
---|
234 | it under the same terms as Perl itself. |
---|
235 | |
---|
236 | =cut |
---|
237 | |
---|
238 | 1; |
---|