1 | package LATMOS::Accounts::Bases::OCHelper::User; |
---|
2 | |
---|
3 | # $Id: User.pm 2932 2010-08-10 17:19:21Z nanardon $ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use base qw(LATMOS::Accounts::Bases::OCHelper); |
---|
8 | use LATMOS::Accounts::Utils; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | LATMOS::Accounts::Bases::OCHelper::User - |
---|
13 | Helper to create user in L<LATMOS::Accounts> system |
---|
14 | |
---|
15 | =cut |
---|
16 | |
---|
17 | sub _step { |
---|
18 | my ($self, $info) = @_; |
---|
19 | if ($info->{step} <= 0) { |
---|
20 | $info->{name}{ask} = 1; |
---|
21 | push(@{$info->{ask}}, qw(givenName sn)); |
---|
22 | $info->{step} = 1; |
---|
23 | if (!($info->{name}{content} || |
---|
24 | ($info->{contents}{sn}) && $info->{contents}{givenName})) { |
---|
25 | return 'NEEDINFO'; |
---|
26 | } |
---|
27 | } |
---|
28 | if ($info->{step} == 1) { |
---|
29 | $info->{step} = 2; |
---|
30 | my $login = $info->{name}{content}; |
---|
31 | if (!$login) { |
---|
32 | $info->{name}{ask} = 1; |
---|
33 | # Try to find a login: |
---|
34 | |
---|
35 | if ($info->{contents}{sn}) { |
---|
36 | my $login = lc(to_ascii($info->{contents}{sn})); |
---|
37 | $login =~ s/ //g; # strip space in login |
---|
38 | length($login) > 10 and $login = substr($login, 0, 8); |
---|
39 | for (1) { |
---|
40 | $self->base->get_object('user', $login) or do { |
---|
41 | $info->{name}{content} = $login; |
---|
42 | last; |
---|
43 | }; |
---|
44 | if ($info->{contents}{givenName}) { |
---|
45 | length($login) > 8 and $login = substr($login, 0, 8); |
---|
46 | $login .= substr(lc(to_ascii($info->{contents}{givenName})), 0, 1); |
---|
47 | $self->base->get_object('user', $login) or do { |
---|
48 | $info->{name}{contents} = $login; |
---|
49 | last; |
---|
50 | }; |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | } else { |
---|
55 | $self->base->get_object('user', $login) and do { |
---|
56 | $info->{name}{ask} = 1; |
---|
57 | } |
---|
58 | } |
---|
59 | push(@{$info->{ask}}, 'uidNumber'); |
---|
60 | push(@{$info->{ask}}, 'gidNumber'); |
---|
61 | my $mailid = $info->{contents}{givenName} && $info->{contents}{sn} |
---|
62 | ? sprintf('%s.%s', |
---|
63 | to_ascii(lc($info->{contents}{givenName})), |
---|
64 | to_ascii(lc($info->{contents}{sn})),) |
---|
65 | : undef; |
---|
66 | $mailid =~ s/ /-/g if ($mailid); # replace space by '-' in mail |
---|
67 | push(@{$info->{ask}}, 'mail'); |
---|
68 | $info->{contents}{mail} = $mailid && $self->base->{defattr}{'user.maildomain'} |
---|
69 | ? sprintf('%s@%s', $mailid, |
---|
70 | $self->base->{defattr}{'user.maildomain'}) |
---|
71 | : undef; |
---|
72 | if (my $attr = $self->base->attribute('user', 'aliases')) { |
---|
73 | if (!$attr->ro) { |
---|
74 | push(@{$info->{ask}}, 'aliases'); |
---|
75 | $info->{contents}{aliases} = $mailid |
---|
76 | } |
---|
77 | } |
---|
78 | if (my $attr = $self->base->attribute('user', 'revaliases')) { |
---|
79 | if (!$attr->ro) { |
---|
80 | push(@{$info->{ask}}, 'revaliases'); |
---|
81 | $info->{contents}{revaliases} = $mailid |
---|
82 | } |
---|
83 | } |
---|
84 | return 'NEEDINFO'; |
---|
85 | |
---|
86 | |
---|
87 | } elsif ($info->{step} == 2) { |
---|
88 | if ($self->base->create_c_object($self->otype, |
---|
89 | $info->{name}{content}, |
---|
90 | %{$info->{contents} || {}}, |
---|
91 | )) { |
---|
92 | return 'CREATED'; |
---|
93 | } else { |
---|
94 | return 'ERROR'; |
---|
95 | } |
---|
96 | } else { |
---|
97 | return undef; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | 1; |
---|
102 | |
---|
103 | __END__ |
---|
104 | |
---|
105 | =head1 SEE ALSO |
---|
106 | |
---|
107 | L<LATMOS::Accounts::Bases::OCHelper> |
---|
108 | |
---|
109 | =head1 AUTHOR |
---|
110 | |
---|
111 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
112 | |
---|
113 | =head1 COPYRIGHT AND LICENSE |
---|
114 | |
---|
115 | Copyright (C) 2012 by Thauvin Olivier |
---|
116 | |
---|
117 | This library is free software; you can redistribute it and/or modify |
---|
118 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
119 | at your option, any later version of Perl 5 you may have available. |
---|
120 | |
---|
121 | =cut |
---|