XPO Tips and Tricks (2) @ CurrentUser

I am still amazed by the amount of neat tips, tricks and new (or sometimes really old) things I am still learning every day about XAF and XPO only by browsing the daily support tickets.

This time I stumbled upon a couple tickets:

https://www.devexpress.com/Support/Center/Question/Details/T214693/how-to-create-currentuser-parameter-instead-of-currentuserid

https://www.devexpress.com/Support/Center/Question/Details/T468768/is-there-a-way-to-pull-in-the-current-user-into-a-security-criteria-expression

And

https://www.devexpress.com/Support/Center/Question/Details/Q415990/criteria-current-user-property

Those ones made me revisit the docs about Free Joins in XPO and honestly I don’t know why I haven’t been using this more often because it is a beauty.

Ok, to the point. Do you need the Current User in your criteria instead of the CurrentUserId?

[<PermissionPolicyUser>][[Oid]=CurrentUserId() And [PropertyInUser]== [^.PropertyInClassBeignUsed]]

Of couse same thing apply to SecuritySystemUser or any other custom class you are using.

Need only to filter by a property of the current user:

[<PermissionPolicyUser>][Oid=CurrentUserId()].Single(PropertytoCheck)

Check how Single() is being used to get the value of PropertyToCheck.

Another route is to create your own Custom Function Criteria.

Let’s say we want to check for the User’s birthday. Something like this:

public class GetCurrentUserBirthdayFunction : ICustomFunctionOperator
{
      public string Name { get { return "GetCurrentUserBirthday "; } }

      public object Evaluate(params object[] operands) 
      { 
        var user = SecuritySystem.CurrentUser as PermissionPolicyUser; 
        if (user != null) 
        { 
           return user.Birthday; 
        } 
        else { return null; } 
      } 

      public Type ResultType(params Type[] operands)
      { 
        return typeof(DateTime); 
      } 
}

And don’t forget to register it in the Module.cs constructor:

CriteriaOperator.RegisterCustomFunction(new GetCurrentUserBirthdayFunction());

That’s it. Now you can call GetCurrentUserBirthday() in your criterias.

Have you guys used the xpo free joins before? If yes, leave a comment with a sample implementation.

Until next time. XPO out!

 

 

Posted in XPO

One thought on “XPO Tips and Tricks (2) @ CurrentUser

Leave a Reply

Your email address will not be published.