import of rt 3.0.9
[freeside.git] / rt / html / REST / 1.0 / Forms / user / default
1 %# REST/1.0/Forms/user/default
2 %#
3 <%ARGS>
4 $id
5 $format => 's'
6 $changes => {}
7 </%ARGS>
8 <%perl>
9 my @comments;
10 my ($c, $o, $k, $e) = ("", [], {}, 0);
11 my %data = %$changes;
12 my $user = new RT::User $session{CurrentUser};
13 my @fields = qw(RealName NickName Gecos Organization Address1 Address2 City
14                 State Zip Country HomePhone WorkPhone MobilePhone PagerPhone
15                 FreeformContactInfo Comments Signature Lang EmailEncoding
16                 WebEncoding ExternalContactInfoId ContactInfoSystem
17                 ExternalAuthId AuthSystem);
18 my %fields = map { lc $_ => $_ } @fields;
19
20 if ($id ne 'new') {
21     $user->Load($id);
22     if (!$user->Id) {
23         return [ "# User $id does not exist.", [], {}, 1 ];
24     }
25 }
26 else {
27     if (%data == 0) {
28         return [
29             "# Required: Name, EmailAddress",
30             [ qw(id Name EmailAddress Organization Password Comments) ],
31             {
32                 id => "user/new",
33                 Name => "",
34                 EmailAddress => "",
35                 Organization => "",
36                 Password => "",
37                 Comments => ""
38             },
39             0
40         ];
41     }
42     else {
43         my %v;
44         my %create = %fields;
45         $create{name}         = "Name";
46         $create{password}     = "Password";
47         $create{emailaddress} = "EmailAddress";
48         $create{contactinfo}  = "FreeformContactInfo";
49         # Do any fields need to be excluded here?
50
51         foreach my $k (keys %data) {
52             if (exists $create{lc $k}) {
53                 $v{$create{lc $k}} = delete $data{$k};
54             }
55         }
56
57         $user->Create(%v);
58         unless ($user->Id) {
59             return [ "# Could not create user.", [], {}, 1 ];
60         }
61
62         $id = $user->Id;
63         delete $data{id};
64         push(@comments, "# User $id created.");
65         goto DONE if %data == 0;
66     }
67 }
68
69 if (%data == 0) {
70     my @data;
71
72     push @data, [ id => "user/".$user->Id ];
73     push @data, [ Name => $user->Name ];
74     push @data, [ Password => '********' ];
75     push @data, [ EmailAddress => $user->EmailAddress ];
76
77     foreach my $key (@fields) {
78         my $val = $user->$key;
79
80         if ($format eq 'l' || (defined $val && $val ne '')) {
81             $key = "ContactInfo" if $key eq 'FreeformContactInfo';
82             push @data, [ $key => $val ];
83         }
84     }
85
86     my %k = map {@$_} @data;
87     $o = [ map {$_->[0]} @data ];
88     $k = \%k;
89 }
90 else {
91     my ($get, $set, $key, $val, $n, $s);
92
93     foreach $key (keys %data) {
94         $val = $data{$key};
95         $key = lc $key;
96         $n = 1;
97
98         if ($key eq 'name' || $key eq 'emailaddress' ||
99             $key eq 'contactinfo' || exists $fields{$key})
100         {
101             if (exists $fields{$key}) {
102                 $key = $fields{$key};
103             }
104             else {
105                 $key = "FreeformContactInfo" if $key eq 'contactinfo';
106                 $key = "EmailAddress" if $key eq 'emailaddress';
107                 $key = "Name" if $key eq 'name';
108             }
109             $set = "Set$key";
110
111             next if $val eq $user->$key;
112             ($n, $s) = $user->$set($val);
113         }
114         elsif ($key eq 'password') {
115             ($n, $s) = $user->SetPassword($val) unless $val =~ /^\**$/;
116         }
117         elsif ($key ne 'id') {
118             $n = 0;
119             $s = "Unknown field.";
120         }
121
122     SET:
123         if ($n == 0) {
124             $e = 1;
125             push @comments, "# $key: $s";
126             unless (@$o) {
127                 my %o = keys %$changes;
128                 delete @o{"id", @fields};
129                 @$o = ("id", @fields, keys %o);
130                 $k = $changes;
131             }
132         }
133     }
134
135     push(@comments, "# User $id updated.") unless $n == 0;
136 }
137
138 DONE:
139 $c ||= join("\n", @comments) if @comments;
140 return [ $c, $o, $k, $e ];
141 </%perl>