How do I delete all session variables at once if they are not in Array?
PS I set them this way:
$this->getUser()->setAttribute('PayPalTransaction.hash', $request->getParameter('hash'));
Regards,
Roman
The sfUser class (which you get with $this->getUser()), keeps all it's attributes in a sfNamespacedParameterHolder. So the setAttribute() function on sfUser if merely a proxy to the sfNamespacedParameterHolder::setAttribute(). You can get the reference to this holder with sfUser::getAttributeHolder().
The sfNamespacedParameterHolder also has a function clear(), which clears all attributes.
So to clear all attributes, use:
$this->getUser()->getAttributeHolder()->clear().
(Please note that you will still be authenticated (e.g. logged in) when you clear the attribute holder).
Another way if you want to remove just one session variable not all of them is to use the following code
$this->getUser()->getAttributeHolder()->remove('att_name');
Again this will only remove one not all ... to clear all use the previous code by Grad
To remove all attributes of a namespace :
$this->getUser()->getAttributeHolder()->removeNamespace('yournamespace');
Related
When i am writing the .save() in grails it is inserting a new row in the db table. However, not persisting data in the object.
I have tried with .save(flush: true) but had no luck.
Please help.
Thanks
Try save(failOnError:true) or check the return value of save() -- which is success/failure in Groovy-truth.
You can also add logSql: true to your application.yml datasource and
logger 'org.hibernate.type.descriptor.sql.BasicBinder', TRACE, ['STDOUT']
logger 'org.hibernate.SQL', TRACE, ['STDOUT']
to your logback.groovy
Probably the object you want to save is not validated. Means that not match with the minimum constraints described on the domain class.
Debug your code and run the validated method, after that take a look on the errors property.
object.validate()
object.errors
Take a look
https://docs.grails.org/latest/ref/Constraints/Usage.html
Remember that all not declared attributes on the constraints closure, are compulsory by default for saving the object
If I have an existing image, how can get a reference to its Image::Info object? In particular, I want to read an option (that was previously set using image.define(...)) from it using the [] operator.
If there is any other way to read a value set via image.define(...) from an image, that would be perfect as well.
The define method is the same as []=, so you can get the option value using [].
I've an array of objects containing title and salary which is used in typeahead directive.
I display department name and get entire object as value.
If none of the options match, I want user entered string to be converted to object still. Is there any way to update that?
This answer is pretty late, but I would just use ng-blur (to trap the end of the users input) along with a variable bound to typeahead-no-results. Then test if the variable is true in the method bound to ng-blur, and, if so, make an object out of the String supplied by the user and push it to your data source. Simple example found here.
I am using valueUnbound method of HttpSessionBindingListener to release lock(an entry from the database), before session is about to expire:
#Override
public void valueUnbound(HttpSessionBindingEvent event) {
String user = (String) event.getSession().getAttribute("currentUsr");
removeLock(user);
}
When the lock is set, I am setting up the username as a session variable.
I need this "username" in my remove lock method. But the getAttribute is throwing an exception:
java.lang.IllegalStateException: getAttribute: Session already invalidated
I need help in getting the session variable?? or is there any other way to get the username?
No, since session has been invalidated.
Although, I figured out the solution, I am setting the attribute via servlet context in
valueBound method and getting it through the : event.getSession().getServletContext().getAttribute("cUser");
it works fine. Thank You EJP
I got your point EJP, you are right , I am making it complex, I can get it from event.getValue() . +1 to your answer, Thank You.
Although, I figured out the solution, I am setting the attribute via servlet context in valueBound method and getting it through the : event.getSession().getServletContext().getAttribute("cUser");
So.. You are storing session scoped data in the application scope. Do you realize that this way the data is shared among all visitors of the webapp? Visitor X would then see the attribute set by visitor Y which has visited the website at a later moment. It makes the problem only worse.
Anyway, as to the concrete problem, as the exception message is trying to tell you, the session has already been invalidated at that point. There are two ways to solve this:
Make currentUsr a property of the class which is implementing HttpSessionBindingListener, so that you don't need to grab it as a distinct session attribute.
Use a HttpSessionListener instead. The sessionDestroyed() method is called right before invalidation, so you should still have access to all attributes.
I tried to do the following:
20 session[:atoken] = linked_in_data['extra']['access_token'].token
21 session[:asecret] = linked_in_data['extra']['access_token'].secre
t
This is within a method inside of a User model.
But I get an error saying undefined method for session...why? Can session variables only be set in a controller?
It's a bad practice, but if you must do it :
http://m.onkey.org/how-to-access-session-cookies-params-request-in-model
But, finding a workaround is always better. Take a look at that as well :
http://media.railscasts.com/videos/119_session_based_model.mov
The session can't be accessed in the models. It would break somehow the MVC structure of the app. If you want to change the session values during save, update etc..., you can try to use sweeper.See the api. You can access the model's attributes, and the session as well, and you can observe changes of the object.