rt 4.2.13 ticket#13852
[freeside.git] / rt / share / html / REST / 1.0 / Forms / user / default
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
6 %#                                          <sales@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 $fields => undef
55 </%ARGS>
56 <%perl>
57 my @comments;
58 my ($c, $o, $k, $e) = ("", [], {}, 0);
59 my %data = %$changes;
60 my $user = RT::User->new($session{CurrentUser});
61
62 my @fields =
63   qw(Name EmailAddress RealName NickName Gecos Organization Address1
64   Address2 City State Zip Country HomePhone WorkPhone MobilePhone PagerPhone
65   FreeformContactInfo Comments Signature Lang EmailEncoding
66   WebEncoding ExternalContactInfoId ContactInfoSystem
67   ExternalAuthId AuthSystem Privileged Disabled);
68
69 if ( $fields && %$fields ) {
70   @fields = grep { exists $fields->{ lc $_ } } @fields;
71 }
72
73 my %fields = map { lc $_ => $_ } @fields;
74
75 if ($id ne 'new') {
76     $user->Load($id);
77     if (!$user->Id) {
78         return [ "# User $id does not exist.", [], {}, 1 ];
79     }
80 }
81 else {
82     if (keys %data == 0) {
83         return [
84             "# Required: Name, EmailAddress",
85             [ qw(id Name EmailAddress Organization Password Comments) ],
86             {
87                 id => "user/new",
88                 Name => "",
89                 EmailAddress => "",
90                 Organization => "",
91                 Password => "",
92                 Comments => ""
93             },
94             0
95         ];
96     }
97     else {
98         my %v;
99         my %create = %fields;
100         $create{name}         = "Name";
101         $create{password}     = "Password";
102         $create{emailaddress} = "EmailAddress";
103         $create{contactinfo}  = "FreeformContactInfo";
104         # Do any fields need to be excluded here?
105
106         foreach my $k (keys %data) {
107             if (exists $create{lc $k}) {
108                 $v{$create{lc $k}} = delete $data{$k};
109             }
110         }
111
112         $user->Create(%v);
113         unless ($user->Id) {
114             return [ "# Could not create user.", [], {}, 1 ];
115         }
116
117         $id = $user->Id;
118         delete $data{id};
119         push(@comments, "# User $id created.");
120         goto DONE if keys %data == 0;
121     }
122 }
123
124 if (keys %data == 0) {
125     my @data;
126
127     push @data, [ id => "user/".$user->Id ];
128
129     unless ( $fields && %$fields && !exists $fields->{'password'} ) {
130         push @data, [ Password => '********' ];
131     }
132
133     for my $key (@fields) {
134         my $val = $user->$key;
135         if (   ( $fields && exists $fields->{ lc $key } )
136             || ( defined $format && $format eq 'l' )
137             || ( defined $val && $val ne '' ) )
138         {
139             $key = "ContactInfo" if $key eq 'FreeformContactInfo';
140             push @data, [ $key => $val ];
141         }
142     }
143
144     # Custom fields
145     my $CustomFields = $user->CustomFields;
146     while ( my $CustomField = $CustomFields->Next() ) {
147         # show cf unless there are specified fields that don't include it
148         next
149             unless ( !%$fields
150             || exists $fields->{ lc "CF-" . $CustomField->Name } );
151         next unless $CustomField->CurrentUserHasRight('SeeCustomField');
152         my $CFvalues = $user->CustomFieldValues( $CustomField->Id );
153         my @CFvalues;
154         while ( my $CFvalue = $CFvalues->Next() ) {
155             push @CFvalues, $CFvalue->Content;
156         }
157         push @data, [ "CF-" . $CustomField->Name => \@CFvalues ];
158     }
159
160     my %k = map {@$_} @data;
161     $o = [ map {$_->[0]} @data ];
162     $k = \%k;
163 }
164 else {
165     my ($get, $set, $key, $val, $n, $s);
166     my $updated;
167     foreach $key (keys %data) {
168         $val = $data{$key};
169         $key = lc $key;
170         $n = 1;
171
172         if ($key eq 'name' || $key eq 'emailaddress' ||
173             $key eq 'contactinfo' || exists $fields{$key})
174         {
175             if (exists $fields{$key}) {
176                 $key = $fields{$key};
177             }
178             else {
179                 $key = "FreeformContactInfo" if $key eq 'contactinfo';
180                 $key = "EmailAddress" if $key eq 'emailaddress';
181                 $key = "Name" if $key eq 'name';
182             }
183             $set = "Set$key";
184
185             next if $val eq $user->$key;
186             ($n, $s) = $user->$set($val);
187         }
188         elsif ($key eq 'password') {
189             ($n, $s) = $user->SetPassword($val) unless $val =~ /^\**$/;
190         }
191         elsif ($key ne 'id') {
192             $n = 0;
193             $s = "Unknown field.";
194         }
195
196     SET:
197         if ($n == 0) {
198             $e = 1;
199             push @comments, "# $key: $s";
200             unless (@$o) {
201                 my %o = keys %$changes;
202                 delete @o{"id", @fields};
203                 @$o = ("id", @fields, keys %o);
204                 $k = $changes;
205             }
206         }
207         else {
208             $updated ||= 1;
209         }
210     }
211
212     push(@comments, "# User $id updated.") if $updated;
213 }
214
215 DONE:
216 $c ||= join("\n", @comments) if @comments;
217 return [ $c, $o, $k, $e ];
218 </%perl>