import rt 3.8.8
[freeside.git] / rt / html / REST / 1.0 / Forms / user / default
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC 
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %# 
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 %# REST/1.0/Forms/user/default
49 %#
50 <%ARGS>
51 $id
52 $format => 's'
53 $changes => {}
54 </%ARGS>
55 <%perl>
56 my @comments;
57 my ($c, $o, $k, $e) = ("", [], {}, 0);
58 my %data = %$changes;
59 my $user = new RT::User $session{CurrentUser};
60 my @fields = qw(RealName NickName Gecos Organization Address1 Address2 City
61                 State Zip Country HomePhone WorkPhone MobilePhone PagerPhone
62                 FreeformContactInfo Comments Signature Lang EmailEncoding
63                 WebEncoding ExternalContactInfoId ContactInfoSystem
64                 ExternalAuthId AuthSystem);
65 my %fields = map { lc $_ => $_ } @fields;
66
67 if ($id ne 'new') {
68     $user->Load($id);
69     if (!$user->Id) {
70         return [ "# User $id does not exist.", [], {}, 1 ];
71     }
72 }
73 else {
74     if (%data == 0) {
75         return [
76             "# Required: Name, EmailAddress",
77             [ qw(id Name EmailAddress Organization Password Comments) ],
78             {
79                 id => "user/new",
80                 Name => "",
81                 EmailAddress => "",
82                 Organization => "",
83                 Password => "",
84                 Comments => ""
85             },
86             0
87         ];
88     }
89     else {
90         my %v;
91         my %create = %fields;
92         $create{name}         = "Name";
93         $create{password}     = "Password";
94         $create{emailaddress} = "EmailAddress";
95         $create{contactinfo}  = "FreeformContactInfo";
96         # Do any fields need to be excluded here?
97
98         foreach my $k (keys %data) {
99             if (exists $create{lc $k}) {
100                 $v{$create{lc $k}} = delete $data{$k};
101             }
102         }
103
104         $user->Create(%v);
105         unless ($user->Id) {
106             return [ "# Could not create user.", [], {}, 1 ];
107         }
108
109         $id = $user->Id;
110         delete $data{id};
111         push(@comments, "# User $id created.");
112         goto DONE if %data == 0;
113     }
114 }
115
116 if (%data == 0) {
117     my @data;
118
119     push @data, [ id => "user/".$user->Id ];
120     push @data, [ Name => $user->Name ];
121     push @data, [ Password => '********' ];
122     push @data, [ EmailAddress => $user->EmailAddress ];
123
124     foreach my $key (@fields) {
125         my $val = $user->$key;
126
127         if ($format eq 'l' || (defined $val && $val ne '')) {
128             $key = "ContactInfo" if $key eq 'FreeformContactInfo';
129             push @data, [ $key => $val ];
130         }
131     }
132
133     my %k = map {@$_} @data;
134     $o = [ map {$_->[0]} @data ];
135     $k = \%k;
136 }
137 else {
138     my ($get, $set, $key, $val, $n, $s);
139
140     foreach $key (keys %data) {
141         $val = $data{$key};
142         $key = lc $key;
143         $n = 1;
144
145         if ($key eq 'name' || $key eq 'emailaddress' ||
146             $key eq 'contactinfo' || exists $fields{$key})
147         {
148             if (exists $fields{$key}) {
149                 $key = $fields{$key};
150             }
151             else {
152                 $key = "FreeformContactInfo" if $key eq 'contactinfo';
153                 $key = "EmailAddress" if $key eq 'emailaddress';
154                 $key = "Name" if $key eq 'name';
155             }
156             $set = "Set$key";
157
158             next if $val eq $user->$key;
159             ($n, $s) = $user->$set($val);
160         }
161         elsif ($key eq 'password') {
162             ($n, $s) = $user->SetPassword($val) unless $val =~ /^\**$/;
163         }
164         elsif ($key ne 'id') {
165             $n = 0;
166             $s = "Unknown field.";
167         }
168
169     SET:
170         if ($n == 0) {
171             $e = 1;
172             push @comments, "# $key: $s";
173             unless (@$o) {
174                 my %o = keys %$changes;
175                 delete @o{"id", @fields};
176                 @$o = ("id", @fields, keys %o);
177                 $k = $changes;
178             }
179         }
180     }
181
182     push(@comments, "# User $id updated.") unless $n == 0;
183 }
184
185 DONE:
186 $c ||= join("\n", @comments) if @comments;
187 return [ $c, $o, $k, $e ];
188 </%perl>