Purpose of AssertRequiredRoles? - servicestack

I am implementing my own RequiredRole attribute called RequiredAnyRole, whereby I pass in a list but the user only has to be in 1 of the roles. I have implemented my own method called HasAnyRole which simply queries based on .Any() instead of .All().
I have then overridden the Execute method to use my method rather than HasAllRoles. The problem is im not sure what the method: AssertRequiredRoles is doing? It doesn't seem to be called?
Should I override that to use .Any() rather then .All() too? Here is the original code:
https://github.com/ServiceStack/ServiceStack/blob/82241fc96e187d12f9db2556aea37cf327813adc/src/ServiceStack.ServiceInterface/RequiredRoleAttribute.cs

AssertRequiredRoles is a static helper method that's can be used by other plugins like RequestLogsService to ensure access is only granted to users with the required roles. It's not called when used as a normal attribute filter.
Once you override Execute you retain full control of what gets executed, so you only need to override what you need.

Related

How to create a custom Routing-Controller method decorator like Authorized

I noticed we were able to create custom param decorators, but I can't seem to find a way to make custom method decorators.
I would like to be able to create my own versions of something similar to Authroized decorator that perform some additional checks. I can then compose them together by adding or removing decorators.
For example, a route may look like this:
#Authorized()
#SomeCheck()
#SomeOtherCheck()
#Post("/someRoute")
public CompleteOnboarding(){
}
#Authorized()
#SomeCheck()
#ADifferentCheck()
#Post("/someRoute")
public CompleteOnboarding(){
}
It seems I can push some fake 'roles' into Authorized and have my authorizationChecker perform the additional checks based on what 'roles' are passed in, but this seemed like a bit of a hack and not very extensible so I thought maybe there is another another way.

JAXB how to remove anything from JDefinedClass

i am using jaxb to generate code from an xsd.
The generated code contains a lot of annotations; for classes and fields.
I am trying to use com.sun.tools.internal.xjc.Plugin to modify the generated code.
In the plugin run() method we are given an Outline class from which we can get ClassOutline. ClassOutline has an JDefinedClass final member which has the info about actual class which will be generated.
If i want to add anything, there are apis in JDefinedClass which can be used. But if i want to remove something, there is no way.
e.g. i cannot clear annotations, because the JDefinedClass.annotations() method returns an UnmodifiableCollection. so i cannot clear it or remove anything from it.
i tried to create another JDefinedClass by invoking the _class method but the ClassOutline.implClass variable is final, so i cannot set it.
how to get a JDefinedClass which does not have any annotations?
is there another phase of code generation which i can trap into to really control the generation of JDefinedClass?
The code model is, indeed mostly "write only". But, speaking of annotations, you have probably missed the methods like com.sun.codemodel.JDefinedClass.removeAnnotation(JAnnotationUse) and com.sun.codemodel.JMethod.removeAnnotation(JAnnotationUse) (implemented from com.sun.codemodel.JAnnotatable.removeAnnotation(JAnnotationUse)).
So they're there. You can remove annotations with the normal CodeModel API.
As I can see, you can also remove fields and methods from classes. So what exactly are you missing?
JDefinedClass.annotations() It return an unmodifiable collection object and you cannot modify them.
So work around for this, you can restrict annotation addition/deletion at class and field level before building JCodeModel.
You need to create a custom Jackson2Annotator class which extends Jackson2Annotator and override their methods according to your requirement.
Following are few methods which are being used for specific type of annotation property:
propertyOrder(OTB JsonPropertyOrder)
propertyInclusion(OTB JsonInclude)
propertyField(can be used for custom defined annotation at field level)
More you can discover by looking Jackson2Annotator class what fit into your need.

How can I implement a Multi-DataTriggerBehavior

How can I implement a Multi-DataTriggerBehavior?
I already have one DataTriggerBehavior in place. But I need a trigger that is based on an event and multiple conditions.
I'd recommend you to create your own Behavior to handle this.
These are the steps for a possible solution.
Because you will need to somehow invoke a VisualState in your Behavior's code behind, you have to change the VisualStateManager defined in your xaml to ExtendedVisualStateManager (see the implementation in this post) as the built-in VisualStateManager's GoToState method doesn't accept Grid as its parameter.
Create a Behavior and attach it to a proper Control. I assume you want to do something when an event of the Control is raised. So you need to create a handler for this in your Behavior code-behind.
Create dependency properties that map the properties defined in your viewmodel and subscribe to all their property changed callbacks. You might need local flags to tell whether all of them are updated or not. If they are, in your event handler, call ExtendedVisualStateManager.GoToElementState. Note that you might also need to create a dependency property to reference your Grid and a few others to map your VisualState names.
Attach the Behavior to your Control in xaml and data-bind all its dependency properties.
Hope this makes sense.

Custom Control Custom Methods?

I have been making good use of custom properties withing custom controls. Is there such thing as custom methods? Say I want something to happen in a CC. A good example is the show method of the dialog box extension. If I have a cc with a extension dialog inside, I want my custom control to have a Show method which insulates the end user programmer from the extension pages Shoe method.
Is there anyway to do this?
At runtime, all Custom Control elements become instances of the UIIncludeComposite class; as such, there are many built in methods that you can call against any given control instance, but there is no way to specify custom methods, as opposed to custom properties.
There are, however, at least two ways you could achieve the result you're after:
Convert your Custom Control to a component (this NotesIn9 episode describes the simplest approach to this process). Once you've migrated the class that Designer generated to one that won't get overridden every time you build your NSF, you can add custom methods without fear that the next build will just wipe them out again. Since Custom Controls are essentially just IBM's implementation of the JSF 2.0 notion of "composite components", you could also create a component from scratch that has the same behavior as your existing Custom Control but also supports custom behavior. Note that either approach does not necessarily require that you create an OSGi library... you can define these components directly in an NSF; you only need to push them to a library if you want to reuse them across multiple NSFs without having to copy the various files to each.
In the custom properties for your control, include one property that accepts an API object. In other words, you could create any object (say, a Java class or SSJS object) that supports the custom methods you wish to define, and pass that object to the control. You could then call those methods by getting a handle on the object via the CC's property map.
For example:
<myCC id="myCustomControl" API="#{someObject}" />
Assuming whatever #{someObject} resolves to includes a show() method, you can call that method by getting a handle on the instance that has been passed to the control:
var cc = getComponent("myCustomControl");
var ccProperties = cc.getPropertyMap();
var ccAPI = ccProperties.get("API");
ccAPI.show(cc);
In the above example, I'm passing the actual Custom Control to the show() method, because the object itself isn't aware of the Custom Control it was passed to. So if that method needs to get a handle on its children to toggle their rendered property, for example, then it needs some other way of determining its context.
Tim's solution with passing in the object is a great solution to that.
Just something that popped into my head, would be easy to make a property similar to the rendered property on a control. Pass in a value and inside the custom control do something based on its value ie. if true display dialog, else hide, in the XPage during run time modify this value and partial refresh the control, the logic will be re run by this and the control will display etc.
Another solution could be to include a JavaScript library in your custom control providing functions (your custom control methods) where you'd have to pass in the id of the custom control instance.

JSF: How do I include parameters in an action method's return string?

I'm writing an action method that will store a new object in a database. Once this is done, I want to navigate to view that newly created object. To do this, I was planning to include a querystring or some sort of parameter in the return String of the action method, but I can't figure out how. If I append a query string manually, it appears that it's being ignored. Also, manually adding parameters by concatenating strings doesn't seem like a good idea to me. Is it possible to do this in a type-safe manner?
The way I've always handled this is to get a reference to the bean which provides the content for the page you'll be displaying, and just set its properties directly. The navigation string returned from an action method isn't meant for passing parameters, but you don't need it to; all they'd be used for is setting bean properties anyway.

Resources