This commit was generated by cvs2svn to compensate for changes in r4407,
[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-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
35 %# 
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
44 %# 
45 %# END BPS TAGGED BLOCK }}}
46 %# REST/1.0/Forms/user/default
47 %#
48 <%ARGS>
49 $id
50 $format => 's'
51 $changes => {}
52 </%ARGS>
53 <%perl>
54 my @comments;
55 my ($c, $o, $k, $e) = ("", [], {}, 0);
56 my %data = %$changes;
57 my $user = new RT::User $session{CurrentUser};
58 my @fields = qw(RealName NickName Gecos Organization Address1 Address2 City
59                 State Zip Country HomePhone WorkPhone MobilePhone PagerPhone
60                 FreeformContactInfo Comments Signature Lang EmailEncoding
61                 WebEncoding ExternalContactInfoId ContactInfoSystem
62                 ExternalAuthId AuthSystem);
63 my %fields = map { lc $_ => $_ } @fields;
64
65 if ($id ne 'new') {
66     $user->Load($id);
67     if (!$user->Id) {
68         return [ "# User $id does not exist.", [], {}, 1 ];
69     }
70 }
71 else {
72     if (%data == 0) {
73         return [
74             "# Required: Name, EmailAddress",
75             [ qw(id Name EmailAddress Organization Password Comments) ],
76             {
77                 id => "user/new",
78                 Name => "",
79                 EmailAddress => "",
80                 Organization => "",
81                 Password => "",
82                 Comments => ""
83             },
84             0
85         ];
86     }
87     else {
88         my %v;
89         my %create = %fields;
90         $create{name}         = "Name";
91         $create{password}     = "Password";
92         $create{emailaddress} = "EmailAddress";
93         $create{contactinfo}  = "FreeformContactInfo";
94         # Do any fields need to be excluded here?
95
96         foreach my $k (keys %data) {
97             if (exists $create{lc $k}) {
98                 $v{$create{lc $k}} = delete $data{$k};
99             }
100         }
101
102         $user->Create(%v);
103         unless ($user->Id) {
104             return [ "# Could not create user.", [], {}, 1 ];
105         }
106
107         $id = $user->Id;
108         delete $data{id};
109         push(@comments, "# User $id created.");
110         goto DONE if %data == 0;
111     }
112 }
113
114 if (%data == 0) {
115     my @data;
116
117     push @data, [ id => "user/".$user->Id ];
118     push @data, [ Name => $user->Name ];
119     push @data, [ Password => '********' ];
120     push @data, [ EmailAddress => $user->EmailAddress ];
121
122     foreach my $key (@fields) {
123         my $val = $user->$key;
124
125         if ($format eq 'l' || (defined $val && $val ne '')) {
126             $key = "ContactInfo" if $key eq 'FreeformContactInfo';
127             push @data, [ $key => $val ];
128         }
129     }
130
131     my %k = map {@$_} @data;
132     $o = [ map {$_->[0]} @data ];
133     $k = \%k;
134 }
135 else {
136     my ($get, $set, $key, $val, $n, $s);
137
138     foreach $key (keys %data) {
139         $val = $data{$key};
140         $key = lc $key;
141         $n = 1;
142
143         if ($key eq 'name' || $key eq 'emailaddress' ||
144             $key eq 'contactinfo' || exists $fields{$key})
145         {
146             if (exists $fields{$key}) {
147                 $key = $fields{$key};
148             }
149             else {
150                 $key = "FreeformContactInfo" if $key eq 'contactinfo';
151                 $key = "EmailAddress" if $key eq 'emailaddress';
152                 $key = "Name" if $key eq 'name';
153             }
154             $set = "Set$key";
155
156             next if $val eq $user->$key;
157             ($n, $s) = $user->$set($val);
158         }
159         elsif ($key eq 'password') {
160             ($n, $s) = $user->SetPassword($val) unless $val =~ /^\**$/;
161         }
162         elsif ($key ne 'id') {
163             $n = 0;
164             $s = "Unknown field.";
165         }
166
167     SET:
168         if ($n == 0) {
169             $e = 1;
170             push @comments, "# $key: $s";
171             unless (@$o) {
172                 my %o = keys %$changes;
173                 delete @o{"id", @fields};
174                 @$o = ("id", @fields, keys %o);
175                 $k = $changes;
176             }
177         }
178     }
179
180     push(@comments, "# User $id updated.") unless $n == 0;
181 }
182
183 DONE:
184 $c ||= join("\n", @comments) if @comments;
185 return [ $c, $o, $k, $e ];
186 </%perl>