#!/usr/bin/perl -w
#
# perl example on how to create a user in OXaaS including userAttributes
# see https://oxpedia.org/wiki/index.php?title=OX_as_a_Service_Provisioning_using_SOAP#Set_OXaaS_permissions
# 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 $oxaascreds = SOAP::Data->type("Credentials")->value(
	\SOAP::Data->value(
		SOAP::Data->name( "login"    => "$ctxadm" ),
		SOAP::Data->name( "password" => "$ctxadmpw" )
	)
);

my $soclt =
  SOAP::Lite->ns("http://soap.reseller.admin.openexchange.com")
  ->proxy( "https://$oxaashost" . "/webservices/OXResellerUserService" );
my $oxaasclt =
  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 )
		)
	);

# we just have the username
my $name        = "auser";

# get users id
my $result = $soclt->getData(
	$context,
	SOAP::Data->value("User")->value(
		\SOAP::Data->value(SOAP::Data->name( "name"     => $name ))
		),
	$oxaascreds);

my $uid = $result->paramsall->{'id'};

# fetch current permissions
$result = $oxaasclt->getPermissions(
	  SOAP::Data->name( "ctxid" => $ctxid ),
	  SOAP::Data->name( "usrid" => $uid ),
          SOAP::Data->name("creds")->value(
                \SOAP::Data->value(
                      SOAP::Data->name( "login"    => $ctxadm ),
                      SOAP::Data->name( "password" => $ctxadmpw )
                )
          )
        );

print Dumper($result->paramsall);

# disable SEND and RECEIVE permissions
my @disablePerms;
push(@disablePerms, SOAP::Data->name( perms => "SEND" ));
# case does not matter
push(@disablePerms, SOAP::Data->name( perms => "Receive" ));

$result = $oxaasclt->disablePermissions(
	  SOAP::Data->name( "ctxid" => $ctxid ),
	  SOAP::Data->name( "usrid" => $uid ),
          SOAP::Data->name( "perms" => @disablePerms ),
          SOAP::Data->name("creds")->value(
                \SOAP::Data->value(
                      SOAP::Data->name( "login"    => $ctxadm ),
                      SOAP::Data->name( "password" => $ctxadmpw )
                )
          )
        );

$result = $oxaasclt->getPermissions(
	  SOAP::Data->name( "ctxid" => $ctxid ),
	  SOAP::Data->name( "usrid" => $uid ),
          SOAP::Data->name("creds")->value(
                \SOAP::Data->value(
                      SOAP::Data->name( "login"    => $ctxadm ),
                      SOAP::Data->name( "password" => $ctxadmpw )
                )
          )
        );

print Dumper($result->paramsall);


