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