diff options
Diffstat (limited to 'rt/html/REST/1.0/Forms/user')
-rw-r--r-- | rt/html/REST/1.0/Forms/user/default | 141 | ||||
-rw-r--r-- | rt/html/REST/1.0/Forms/user/ns | 41 |
2 files changed, 182 insertions, 0 deletions
diff --git a/rt/html/REST/1.0/Forms/user/default b/rt/html/REST/1.0/Forms/user/default new file mode 100644 index 000000000..6b216e072 --- /dev/null +++ b/rt/html/REST/1.0/Forms/user/default @@ -0,0 +1,141 @@ +%# REST/1.0/Forms/user/default +%# +<%ARGS> +$id +$format => 's' +$changes => {} +</%ARGS> +<%perl> +my @comments; +my ($c, $o, $k, $e) = ("", [], {}, 0); +my %data = %$changes; +my $user = new RT::User $session{CurrentUser}; +my @fields = qw(RealName NickName Gecos Organization Address1 Address2 City + State Zip Country HomePhone WorkPhone MobilePhone PagerPhone + FreeformContactInfo Comments Signature Lang EmailEncoding + WebEncoding ExternalContactInfoId ContactInfoSystem + ExternalAuthId AuthSystem); +my %fields = map { lc $_ => $_ } @fields; + +if ($id ne 'new') { + $user->Load($id); + if (!$user->Id) { + return [ "# User $id does not exist.", [], {}, 1 ]; + } +} +else { + if (%data == 0) { + return [ + "# Required: Name, EmailAddress", + [ qw(id Name EmailAddress Organization Password Comments) ], + { + id => "user/new", + Name => "", + EmailAddress => "", + Organization => "", + Password => "", + Comments => "" + }, + 0 + ]; + } + else { + my %v; + my %create = %fields; + $create{name} = "Name"; + $create{password} = "Password"; + $create{emailaddress} = "EmailAddress"; + $create{contactinfo} = "FreeformContactInfo"; + # Do any fields need to be excluded here? + + foreach my $k (keys %data) { + if (exists $create{lc $k}) { + $v{$create{lc $k}} = delete $data{$k}; + } + } + + $user->Create(%v); + unless ($user->Id) { + return [ "# Could not create user.", [], {}, 1 ]; + } + + $id = $user->Id; + delete $data{id}; + push(@comments, "# User $id created."); + goto DONE if %data == 0; + } +} + +if (%data == 0) { + my @data; + + push @data, [ id => "user/".$user->Id ]; + push @data, [ Name => $user->Name ]; + push @data, [ Password => '********' ]; + push @data, [ EmailAddress => $user->EmailAddress ]; + + foreach my $key (@fields) { + my $val = $user->$key; + + if ($format eq 'l' || (defined $val && $val ne '')) { + $key = "ContactInfo" if $key eq 'FreeformContactInfo'; + push @data, [ $key => $val ]; + } + } + + my %k = map {@$_} @data; + $o = [ map {$_->[0]} @data ]; + $k = \%k; +} +else { + my ($get, $set, $key, $val, $n, $s); + + foreach $key (keys %data) { + $val = $data{$key}; + $key = lc $key; + $n = 1; + + if ($key eq 'name' || $key eq 'emailaddress' || + $key eq 'contactinfo' || exists $fields{$key}) + { + if (exists $fields{$key}) { + $key = $fields{$key}; + } + else { + $key = "FreeformContactInfo" if $key eq 'contactinfo'; + $key = "EmailAddress" if $key eq 'emailaddress'; + $key = "Name" if $key eq 'name'; + } + $set = "Set$key"; + + next if $val eq $user->$key; + ($n, $s) = $user->$set($val); + } + elsif ($key eq 'password') { + ($n, $s) = $user->SetPassword($val) unless $val =~ /^\**$/; + } + elsif ($key ne 'id') { + $n = 0; + $s = "Unknown field."; + } + + SET: + if ($n == 0) { + $e = 1; + push @comments, "# $key: $s"; + unless (@$o) { + my %o = keys %$changes; + delete @o{"id", @fields}; + @$o = ("id", @fields, keys %o); + $k = $changes; + } + } + } + + push(@comments, "# User $id updated.") unless $n == 0; +} + +DONE: +$c ||= join("\n", @comments) if @comments; +return [ $c, $o, $k, $e ]; +</%perl> diff --git a/rt/html/REST/1.0/Forms/user/ns b/rt/html/REST/1.0/Forms/user/ns new file mode 100644 index 000000000..36b323746 --- /dev/null +++ b/rt/html/REST/1.0/Forms/user/ns @@ -0,0 +1,41 @@ +%# BEGIN LICENSE BLOCK +%# +%# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com> +%# +%# (Except where explictly superceded by other copyright notices) +%# +%# This work is made available to you under the terms of Version 2 of +%# the GNU General Public License. A copy of that license should have +%# been provided with this software, but in any event can be snarfed +%# from www.gnu.org. +%# +%# This work is distributed in the hope that it will be useful, but +%# WITHOUT ANY WARRANTY; without even the implied warranty of +%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +%# General Public License for more details. +%# +%# Unless otherwise specified, all modifications, corrections or +%# extensions to this work which alter its source code become the +%# property of Best Practical Solutions, LLC when submitted for +%# inclusion in the work. +%# +%# +%# END LICENSE BLOCK +%# REST/1.0/Forms/user/ns +%# +<%ARGS> +$id +</%ARGS> +<%perl> +use RT::Users; + +my $field = "Name"; +$field = "EmailAddress" if $id =~ /\@/; + +my $users = new RT::Users $session{CurrentUser}; +$users->Limit(FIELD => $field, OPERATOR => '=', VALUE => $id); +if ($users->Count == 0) { + return (0, "No user named $id exists."); +} +return $users->Next->Id; +</%perl> |