Line # Revision Author
1 196 ahitrov package users::UserProfile;
2
3 use base "Contenido::Document";
4 use Digest::MD5;
5 use Contenido::Globals;
6
7 305 ahitrov my %CREDENTIAL_FIELDS = (
8 'users::Email' => 'email',
9 'users::Phone' => 'phone',
10 'users::OA::VK' => 'vkontakte',
11 'users::OA::FaceBook' => 'facebook',
12 'users::OA::Google' => 'google',
13 'users::OA::Mailru' => 'mailru',
14 );
15
16 310 ahitrov my %CREDENTIAL_REVERSE = (
17 'email' => 'users::Email',
18 'phone' => 'users::Phone',
19 'vkontakte' => 'users::OA::VK',
20 'facebook' => 'users::OA::FaceBook',
21 'google' => 'users::OA::Google',
22 'mailru' => 'users::OA::Mailru',
23 );
24
25 196 ahitrov sub extra_properties
26 {
27 return (
28 258 ahitrov { 'attr' => 'status', 'type' => 'status', 'rusname' => 'Статус пользователя',
29 196 ahitrov 'cases' => [
30 [0, 'Блокированный'],
31 [1, 'Активный'],
32 [5, 'Временная активация'],
33 ],
34 },
35 258 ahitrov { 'attr' => 'type', 'type' => 'status', 'rusname' => 'Тип аккаунта',
36 'cases' => [
37 [0, 'Обычный пользователь'],
38 [1, 'Продвинутый пользователь'],
39 [2, 'Модератор'],
40 [10, 'Администратор'],
41 ],
42 },
43 196 ahitrov { 'attr' => 'visibility', 'type' => 'status', 'rusname' => 'Область видимости',
44 'cases' => [
45 [0, 'Данные моего аккаунта видны только мне'],
46 [1, 'Данные моего аккаунта видны всем'],
47 [2, 'Данные моего аккаунта видны друзьям'],
48 [3, 'Данные моего аккаунта видны друзьям и членам клубов'],
49 ],
50 },
51 { 'attr' => 'country', 'type' => 'string', 'rusname' => 'Страна' },
52 { 'attr' => 'passwd', 'type' => 'password', 'rusname' => 'Пароль', 'rem' => '(<font color="red">Не отображается. Указывать при создании и для изменения</font>)' },
53 { 'attr' => 'confirm', 'type' => 'string', 'rusname' => 'Код подтверждения', hidden => 1 },
54 { 'attr' => 'secmail', 'type' => 'string', 'rusname' => 'E-mail (secondary)' },
55 { 'attr' => 'q1', 'type' => 'string', 'rusname' => 'Контрольный вопрос 1' },
56 { 'attr' => 'a1', 'type' => 'string', 'rusname' => 'Контрольный ответ 1' },
57 { 'attr' => 'q2', 'type' => 'string', 'rusname' => 'Контрольный вопрос 2' },
58 { 'attr' => 'a2', 'type' => 'string', 'rusname' => 'Контрольный ответ 2' },
59 { 'attr' => 'account', 'type' => 'string', 'rusname' => 'Сумма на счете' },
60 { 'attr' => 'interests', 'type' => 'text', 'rusname' => 'Жизненные интересы', rows => 10 },
61 { 'attr' => 'origin', 'type' => 'text', 'rusname' => 'Ориджин', rows => 4 },
62 258 ahitrov { 'attr' => 'avatar', 'type' => 'image', 'rusname' => 'Аватар', crop => ['32x32','150x150'], preview => ['200x200'] },
63 196 ahitrov )
64 }
65
66 305 ahitrov
67 sub post_init {
68 my $self = shift;
69 my $opts = shift;
70
71 $self->{passwd_prev} = $self->passwd;
72
73 return if exists $opts->{ids} || exists $opts{names} || exists $opts{light};
74 if ( $self->id && $state->{users}->use_credentials ) {
75 310 ahitrov $self->{credentials_available} = {};
76 305 ahitrov my $creds = $keeper->get_documents(
77 uid => $self->id,
78 table => 'users::SQL::CredentialsTable',
79 return_mode => 'array_ref',
80 );
81 if ( @$creds ) {
82 my %creds;
83 foreach my $cred ( @$creds ) {
84 $cred->{keeper} = undef;
85 my $main_field = $CREDENTIAL_FIELDS{$cred->class} if exists $CREDENTIAL_FIELDS{$cred->class};
86 310 ahitrov $self->{credentials_available}{$main_field} = 1;
87 305 ahitrov if ( $main_field ) {
88 my $multi_field = $main_field.'s';
89 $self->{$multi_field} = [] unless exists $self->{$multi_field};
90 push @{$self->{$multi_field}}, $cred;
91 $self->{$main_field} = $cred if $cred->main;
92 }
93 }
94 }
95 }
96 return;
97 }
98
99
100 238 ahitrov sub name_full
101 {
102 my $self = shift;
103 my $name = $self->name;
104 if ( $name =~ /^(.*?),[\ \t]+(.*)$/ ) {
105 $name = $2.' '.$1;
106 }
107 return $name;
108 }
109
110 sub name_part
111 {
112 my $self = shift;
113 my $name = $self->name;
114 if ( $name =~ /^(.*?),[\ \t]+(.*)$/ ) {
115 $name = $2;
116 }
117 return $name;
118 }
119
120 sub name_family
121 {
122 my $self = shift;
123 my $name = $self->name;
124 if ( $name =~ /^(.*?),[\ \t]+(.*)$/ ) {
125 $name = $1;
126 }
127 return $name;
128 }
129
130 196 ahitrov sub class_name
131 {
132 return 'Профиль пользователя';
133 }
134
135 sub class_description
136 {
137 return 'Профиль пользователя';
138 }
139
140 sub search_fields
141 {
142 return ('email', 'name', 'login');
143 }
144
145 sub class_table
146 {
147 return 'users::SQL::UserProfile';
148 }
149
150 sub contenido_status_style
151 {
152 my $self = shift;
153 if ( $self->status == 2 ) {
154 return 'color:green;';
155 } elsif ( $self->status == 3 ) {
156 return 'color:olive;';
157 } elsif ( $self->status == 4 ) {
158 return 'color:green;';
159 } elsif ( $self->status == 5 ) {
160 return 'color:red;';
161 }
162 }
163
164 305 ahitrov
165 310 ahitrov sub get_credentials {
166 my ($self, $name, %opts) = @_;
167 my $objects;
168 if ( $name ) {
169 return undef unless exists $CREDENTIAL_REVERSE{$name};
170 my $names = $name.'s';
171 if ( exists $self->{credentials_available}{$name} ) {
172 $objects = $self->{$names};
173 }
174 } elsif ( my @keys = keys %{$self->{credentials_available}} ) {
175 $objects = {};
176 foreach my $key ( @keys ) {
177 my $names = $key.'s';
178 push $objects->{$key} = $self->{$names};
179 }
180 }
181 return $objects;
182 }
183
184 305 ahitrov sub confirm_credential {
185 my ($self, %opts) = @_;
186 my $object;
187 if ( exists $opts{confirm} && $opts{name} && $opts{class} ) {
188 ($object) = $self->keeper->get_documents(
189 class => $opts{class},
190 uid => $self->id,
191 name => lc($opts{name}),
192 limit => 1,
193 );
194 if ( ref $object && $object->confirm eq $opts{confirm} ) {
195 $object->status(1);
196 $object->store;
197 }
198 } elsif ( $opts{name} && $opts{class} ) {
199 ($object) = $self->keeper->get_documents(
200 class => $opts{class},
201 uid => $self->id,
202 name => lc($opts{name}),
203 limit => 1,
204 );
205 if ( ref $object ) {
206 $object->status(1);
207 $object->store;
208 }
209 }
210 return $object;
211 }
212
213
214 sub create_credential {
215 my ($self, %opts) = @_;
216 my $object;
217 if ( $opts{vkontakte} ) {
218 ($object) = $self->keeper->get_documents(
219 306 ahitrov class => 'users::OA::VK',
220 305 ahitrov uid => $self->id,
221 ext_id => $opts{vkontakte},
222 limit => 1,
223 );
224 310 ahitrov return undef if ref $object && $object->uid != $self->id;
225 305 ahitrov unless ( ref $object ) {
226 $object = users::OA::VK->new ($keeper);
227 $object->name( $opts{name} );
228 $object->status( 1 );
229 $object->opaque( $opts{opaque} || 0 );
230 $object->uid( $self->id );
231 $object->ava_url( $opts{avatar} );
232 $object->store;
233 }
234 } elsif ( $opts{facebook} ) {
235 ($object) = $self->keeper->get_documents(
236 306 ahitrov class => 'users::OA::FaceBook',
237 305 ahitrov uid => $self->id,
238 ext_id => $opts{facebook},
239 limit => 1,
240 );
241 310 ahitrov return undef if ref $object && $object->uid != $self->id;
242 305 ahitrov unless ( ref $object ) {
243 $object = users::OA::FaceBook->new ($keeper);
244 $object->name( $opts{name} );
245 $object->status( 1 );
246 $object->opaque( $opts{opaque} || 0 );
247 $object->uid( $self->id );
248 $object->ava_url( $opts{avatar} );
249 $object->store;
250 }
251 } elsif ( $opts{google} ) {
252 ($object) = $self->keeper->get_documents(
253 306 ahitrov class => 'users::OA::Google',
254 305 ahitrov uid => $self->id,
255 ext_id => $opts{google},
256 limit => 1,
257 );
258 310 ahitrov return undef if ref $object && $object->uid != $self->id;
259 305 ahitrov unless ( ref $object ) {
260 $object = users::OA::Google->new ($keeper);
261 $object->name( $opts{name} );
262 if ( $opts{email} ) {
263 $object->email( $opts{email} );
264 $self->create_credential( email => $opts{email}, status => 1 );
265 }
266 $object->status( 1 );
267 $object->opaque( $opts{opaque} || 0 );
268 $object->uid( $self->id );
269 $object->ava_url( $opts{avatar} );
270 $object->store;
271 }
272 } elsif ( $opts{mailru} ) {
273 ($object) = $self->keeper->get_documents(
274 306 ahitrov class => 'users::OA::Mailru',
275 305 ahitrov uid => $self->id,
276 ext_id => $opts{mailru},
277 limit => 1,
278 );
279 310 ahitrov return undef if ref $object && $object->uid != $self->id;
280 305 ahitrov unless ( ref $object ) {
281 $object = users::OA::Mailru->new ($keeper);
282 $object->name( $opts{name} );
283 if ( $opts{email} ) {
284 $object->email( $opts{email} );
285 $self->create_credential( email => $opts{email}, status => 1 );
286 }
287 $object->status( 1 );
288 $object->opaque( $opts{opaque} || 0 );
289 $object->uid( $self->id );
290 $object->ava_url( $opts{avatar} );
291 $object->store;
292 }
293 } elsif ( $opts{email} ) {
294 ($object) = $self->keeper->get_documents(
295 306 ahitrov class => 'users::Email',
296 305 ahitrov name => lc($opts{email}),
297 limit => 1,
298 );
299 310 ahitrov return undef if ref $object && $object->uid != $self->id;
300 305 ahitrov unless ( ref $object ) {
301 $object = users::Email->new ($keeper);
302 $object->name( lc($opts{email}) );
303 $object->name_orig( $opts{email} );
304 $object->main( $opts{main} || 0 );
305 $object->status( $opts{status} || 0 );
306 $object->opaque( $opts{opaque} || 0 );
307 $object->uid( $self->id );
308 $object->confirm( Digest::MD5::md5_hex(int(rand(1000000)), $self->id, $opts{email}) );
309 $object->store;
310 }
311 } elsif ( $opts{phone} ) {
312 ($object) = $self->keeper->get_documents(
313 306 ahitrov class => 'users::Phone',
314 305 ahitrov name => $keeper->{users}->_phone_reduction( $opts{phone} ),
315 limit => 1,
316 );
317 310 ahitrov return undef if ref $object && $object->uid != $self->id;
318 305 ahitrov unless ( ref $object ) {
319 $object = users::Phone->new ($keeper);
320 $object->name( $keeper->{users}->_phone_reduction($opts{phone}) );
321 $object->name_format( $keeper->{users}->_phone_format($opts{phone}) );
322 $object->name_orig( $opts{phone} );
323 $object->main( $opts{main} || 0 );
324 $object->status( $opts{status} || 0 );
325 $object->opaque( $opts{opaque} || 0 );
326 $object->uid( $self->id );
327 $object->confirm( Digest::MD5::md5_hex(int(rand(1000000)), $self->id, $opts{email}) );
328 $object->store;
329 }
330 }
331 return $object;
332 }
333
334
335 196 ahitrov sub pre_store
336 {
337 my $self = shift;
338
339 my $up = $self->{keeper}->get_document_by_id ( $self->id,
340 class => $self->class,
341 );
342 305 ahitrov my $passwd_prev = $self->{passwd_prev} || '';
343 if ( $self->passwd && $self->passwd ne $passwd_prev ) {
344 196 ahitrov warn "Pass = ".$self->passwd."\n" if $DEBUG;
345 my $pass = Digest::MD5::md5_hex($self->passwd);
346 warn "Pass_hex = $pass\n" if $DEBUG;
347 $self->passwd($pass);
348 305 ahitrov } else {
349 $self->passwd($passwd_prev);
350 196 ahitrov }
351 305 ahitrov $self->login( lc($self->login) );
352 196 ahitrov
353 305 ahitrov if ( $state->{users}->use_credentials ) {
354 foreach my $prop ( $self->structure ) {
355 my $name = $prop->{attr};
356 if ( ref $self->$name =~ /^users::OA::/ ) {
357 my $obj = $self->$name;
358 $self->$name( $obj->ext_id );
359 } elsif ( ref $self->$name =~ /^users::/ ) {
360 my $obj = $self->$name;
361 $self->$name( $obj->name );
362 }
363 }
364 } else {
365 $self->email( $keeper->{users}->_email_reduction($self->email) );
366 }
367
368 196 ahitrov my $default_section = $project->s_alias->{users} if ref $project->s_alias eq 'HASH' && exists $project->s_alias->{users};
369 if ( $default_section ) {
370 my $sections = $self->{sections};
371 if ( ref $sections eq 'ARRAY' && scalar @$sections ) {
372 my @new_sects = grep { $_ != $default_section } @$sections;
373 push @new_sects, $default_section;
374 $self->sections(@new_sects);
375 } elsif ( $sections && !ref $sections && $sections != $default_section ) {
376 my @new_sects = ($default_section, $sections);
377 $self->sections(@new_sects);
378 } else {
379 $self->sections($default_section);
380 }
381 }
382
383 return 1;
384 }
385 310 ahitrov
386
387 sub post_delete
388 {
389 my $self = shift;
390
391 if ( $state->{users}->use_credentials ) {
392 my $creds = $keeper->get_documents(
393 table => 'users::SQL::CredentialsTable',
394 uid => $self->id,
395 return_mode => 'array_ref',
396 );
397 map { $_->delete( attachments => 1 ) } @$creds;
398 }
399 1;
400 }
401
402 196 ahitrov 1;