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 | # Such function must not be here, but in LATMOS::Accounts |
---|
123 | # But code does not allow this at time |
---|
124 | sub list_unowned_aliases { |
---|
125 | my ($self, $filter) = @_; |
---|
126 | my $db = $self->db; |
---|
127 | my $sth = $db->db->prepare_cached(q{ |
---|
128 | select name, forward from aliases where |
---|
129 | name not in (select name from "user") |
---|
130 | and |
---|
131 | forward not in (select array[name] from "user") |
---|
132 | } . |
---|
133 | ($filter |
---|
134 | ? q{ |
---|
135 | and (lower(name) ILIKE $1 or |
---|
136 | lower(array_to_string(forward, ',')) ILIKE $1) |
---|
137 | } |
---|
138 | : '') |
---|
139 | ); |
---|
140 | |
---|
141 | $sth->execute($filter ? ('%' . $filter . '%') : ()); |
---|
142 | my %aliases; |
---|
143 | while (my $res = $sth->fetchrow_hashref) { |
---|
144 | $aliases{$res->{name}} = $res->{forward}; |
---|
145 | } |
---|
146 | return \%aliases |
---|
147 | } |
---|
148 | |
---|
149 | sub sync_access { |
---|
150 | my ($self) = @_; |
---|
151 | $self->SUPER::sync_access |
---|
152 | } |
---|
153 | |
---|
154 | sub sync { |
---|
155 | my ($self) = @_; |
---|
156 | $self->default_synchro() |
---|
157 | } |
---|
158 | |
---|
159 | =head1 AUTHOR |
---|
160 | |
---|
161 | Thauvin Olivier |
---|
162 | |
---|
163 | =head1 LICENSE |
---|
164 | |
---|
165 | This library is free software, you can redistribute it and/or modify |
---|
166 | it under the same terms as Perl itself. |
---|
167 | |
---|
168 | =cut |
---|
169 | |
---|
170 | 1; |
---|