#!/usr/bin/perl -w
#
# perl example on how to create a user in OXaaS including userAttributes and unified quota
# see https://oxpedia.org/wiki/index.php?title=OX_as_a_Service_Provisioning_using_SOAP#Create_users for
# further details
#
use strict;
use SOAP::Lite +trace => 'debug';
use Data::Dumper;

BEGIN { $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0 }

my $oxaashost = "CHANGEME";
my $ctxadm    = "CHANGEME";
my $ctxadmpw  = "CHANGEME";
my $ctxid     = CHANGEME;
my $modaccess = "groupware_premium";

my $oxaascreds = SOAP::Data->type("Credentials")->value(
	\SOAP::Data->value(
		SOAP::Data->name( "login"    => "$ctxadm" ),
		SOAP::Data->name( "password" => "$ctxadmpw" )
	)
);

my $soapusr =
  SOAP::Lite->ns("http://soap.reseller.admin.openexchange.com")
  ->proxy( "https://$oxaashost" . "/webservices/OXResellerUserService" );
my $soapoxaas =
  SOAP::Lite->ns("http://soap.oxaas.admin.openexchange.com/")
  ->proxy( "https://$oxaashost" . "/webservices/OXaaSService" );

my $context = SOAP::Data->type("Context")->value(
		\SOAP::Data->value(
			SOAP::Data->name( "id"     => $ctxid )
		)
	);

my $name        = "auser";
my $password    = "secret";
my $displayname = "A User";
my $surname     = "A";
my $gname       = "User";
my $email       = "auser\@example.com";
my $lang        = "de_DE";
my $timezone    = "Europe/Berlin";
my $edition     = "pro_l";
# ONLY for unified quota
my $maxQuota    = 123456;

my @soapdata;
push @soapdata, SOAP::Data->name( "name"                 => $name )->type('string');
push @soapdata, SOAP::Data->name( "password"             => $password );
push @soapdata,	SOAP::Data->name( "display_name"         => $displayname )->type('string');
push @soapdata, SOAP::Data->name( "sur_name"             => $surname )->type('string');
push @soapdata,	SOAP::Data->name( "given_name"           => $gname )->type('string');
push @soapdata, SOAP::Data->name( "primaryEmail"         => $email );
push @soapdata, SOAP::Data->name( "email1"               => $email );
push @soapdata, SOAP::Data->name( "defaultSenderAddress" => $email );
push @soapdata, SOAP::Data->name( "language"             => $lang );
push @soapdata, SOAP::Data->name( "timezone"             => $timezone );
# use code below ONLY if unified quota should be used
push @soapdata, SOAP::Data->name( "maxQuota"             => $maxQuota );

my $edttype = SOAP::Data->name(
	"entries" => \SOAP::Data->value(
		SOAP::Data->name( "key"   => "oxaas_edition_type" ),
		SOAP::Data->name( "value" => $edition )
	)
);

# use code below ONLY if unified quota should be used
my $qunified = SOAP::Data->name(
	"entries" => \SOAP::Data->value(
		SOAP::Data->name( "key"   => "com.openexchange.unifiedquota.enabled" ),
		SOAP::Data->name( "value" => "true" )
	)
);

push @soapdata,
	SOAP::Data->name(
	"userAttributes" => \SOAP::Data->value(
		SOAP::Data->name(
			"entries" => \SOAP::Data->value(
				      SOAP::Data->name( "key" => "oxaas" ),
				      SOAP::Data->name( "value" => \SOAP::Data->value($edttype))
				     ), # use code below ONLY if unified quota should be used
				     \SOAP::Data->value(
                                      SOAP::Data->name( "key" => "config" ),
                                      SOAP::Data->name( "value" => \SOAP::Data->value($qunified))
                                     )
			)
		)
	);

my $result = $soapusr->createByModuleAccessName(
	$context,
	SOAP::Data->value("User")->value(
		\SOAP::Data->value(@soapdata)
		),
	$modaccess,
	$oxaascreds
);

if( $result->fault() ) {
        print $result->faultstring()."\n";
	exit(1);
}

$result = $soapusr->getData(
	$context,
	SOAP::Data->value("User")->value(
		\SOAP::Data->value(@soapdata)
		),
	$oxaascreds);

if( $result->fault() ) {
        print $result->faultstring()."\n";
	exit(1);
}

print Dumper($result->paramsall);

my @results = $result->paramsall;

# set email quota for the new user or, if unified quota is enabled, activate
# unified quota valid for mail and filestorage
$result = $soapoxaas->setMailQuota(
                        SOAP::Data->name( "ctxid" => $ctxid ),
                        SOAP::Data->name( "usrid" => $results[0]->{'id'} ),
                        SOAP::Data->name( "quota" => $maxQuota ),
                        SOAP::Data->name("creds")->value(
                                \SOAP::Data->value(
                                        SOAP::Data->name( "login"    => $ctxadm ),
                                        SOAP::Data->name( "password" => $ctxadmpw )
                                )
                        )
                );

print Dumper($result->paramsall);

