You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@syncope.apache.org by thinkingmik <mi...@gmail.com> on 2020/10/07 08:48:15 UTC

Flowable set user plain attribute

Hello,

I'm using Syncope 2.1.4 and I'm trying to set a custom plain attribute named
/tempPassword/ to an user using *Flowable* without success. This custom
attribute was added before to the schemas and added to the base user object.
It is writable and contains plain text.
I have a groovy /Script task/ like the follow:

/*** BEGIN SCRIPT ***/
import org.apache.syncope.common.lib.patch.AttrPatch;
import org.apache.syncope.common.lib.types.PatchOperation;
import org.apache.syncope.common.lib.to.AttrTO;
import org.apache.syncope.common.lib.types.CipherAlgorithm;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.flowable.impl.FlowableRuntimeUtils;
import org.apache.syncope.common.lib.patch.UserPatch;

User user = execution.getVariable(FlowableRuntimeUtils.USER, User.class);

String alphabet =
(('A'..'N')+('P'..'Z')+('a'..'k')+('m'..'z')+('2'..'9')).join() ;
int n = 12;
def password = new Random().with { (1..n).collect { alphabet[ nextInt(
alphabet.length() ) ] }.join(); }

user.setPassword(password, CipherAlgorithm.SSHA256);
user.setMustChangePassword(true);

user.getPlainAttrs().add(new
AttrPatch.Builder().operation(PatchOperation.ADD_REPLACE).attrTO(createAttrTO("tempPassword",
password)));

execution.setVariable(FlowableRuntimeUtils.USER, user);

def AttrTO createAttrTO(String key, String value) {
	return new AttrTO.Builder().schema(key).value(value).build();
}
/*** END SCRIPT ***/

The /password/ and the /mustChangePassword/ attributes are correctly
assigned to the user but I don't find a way to set any plain attributes.



-----
Michele
--
Sent from: http://syncope-user.1051894.n5.nabble.com/

Re: Flowable set user plain attribute

Posted by thinkingmik <mi...@gmail.com>.
Hi, I have found the way to set plain attributes on user by adding a /service
task/ (with delegate expression ${update}) after my /script task/.

So, my script changed to:

/*** BEGIN SCRIPT ***/
import org.apache.syncope.common.lib.patch.AttrPatch;
import org.apache.syncope.common.lib.types.PatchOperation;
import org.apache.syncope.common.lib.to.AttrTO;
import org.apache.syncope.common.lib.types.CipherAlgorithm;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.flowable.impl.FlowableRuntimeUtils;
import org.apache.syncope.common.lib.patch.UserPatch;
import org.apache.syncope.common.lib.patch.PasswordPatch;
import org.apache.syncope.common.lib.patch.BooleanReplacePatchItem;
import org.apache.syncope.common.lib.patch.PasswordPatch;

User user = execution.getVariable(FlowableRuntimeUtils.USER, User.class);

def password = generatePassword(12);

UserPatch userPatch = new UserPatch();
userPatch.setKey(user.getKey());

BooleanReplacePatchItem boolPatch = new BooleanReplacePatchItem();
boolPatch.setValue(true);
userPatch.setMustChangePassword(boolPatch);

PasswordPatch passwordPatch = new PasswordPatch();
passwordPatch.setValue(password);
passwordPatch.setOnSyncope(true);
userPatch.setPassword(passwordPatch);

userPatch.getPlainAttrs().add(createUpdatePatch("tempPassword", password));

execution.setVariable(FlowableRuntimeUtils.USER_PATCH, userPatch);

def String generatePassword(int n) {
	String alphabet =
(('A'..'N')+('P'..'Z')+('a'..'k')+('m'..'z')+('2'..'9')).join();
	return new Random().with { (1..n).collect { alphabet[ nextInt(
alphabet.length() ) ] }.join(); }
}

def AttrPatch createUpdatePatch(String key, String value) {
    return new
AttrPatch.Builder().operation(PatchOperation.ADD_REPLACE).attrTO(createAttrTO(key,
value)).build();
}

def AttrTO createAttrTO(String key, String value) {
	return new AttrTO.Builder().schema(key).value(value).build();
}
/*** END SCRIPT ***/

<http://syncope-user.1051894.n5.nabble.com/file/t339163/Capture.png> 

Thank you anyway

-----
Michele
--
Sent from: http://syncope-user.1051894.n5.nabble.com/