Log any action made by users in sfDoctrineGuard - symfony1

I need to log any action made by users in sfDoctrineGuard plugin. Basically I'll need to log:
module/action
date
IP from where users are accessing the application
Any plugin? Is that possible? How?

This could be probably the plugin you need, sfDoctrineGuardLoginHistoryPlugin and allows to extend the information that you save.
Check for more plugins here.
Take a look at the code of the plugin, you just need to change the following file: PluginUserLoginHistoryTable.class.php
Add in the function writeLoginHistory and createHistoryEntry the information you want:
writeLoginHistory(sfEvent $event) {
//... same code than in the plugin
//lets save module and action
if (!isset($request) )
{
$sActionName = sfContext::getInstance()->getActionName();
$sModuleName = sfContext::getInstance()->getModuleName();
}
else
{
if (isset($request["module"]))
{
$sActionName = $request["action"];
$sModuleName = $request["module"];
}
}
//get values from the http bar URL
if (!isset($sModuleName))
{
$sFullURL = sfContext::getInstance()->getRouting()->getCurrentInternalUri();
///... strip action and module name from the above URL
}
}
Remember to pass those values to createHistoryEntry function and also to update that function with more input values to be saved.

Related

Working with parsed model in Xtend validator

I want to know how I can access the parsed model of my program. I have a validation check written in xtend which accepts a rule A as it parameter. however I want to search the entire parsed tree and make sure that any other reference to this specific instance of A follows certain specifications.
#Check
def checkActionBelongsToAssociatedRole(ActionDsc act){
var pRole = act.parentRole
var rs = new ResourceSetImpl()
//DONT KNOW IF THIS IS RIGHT
var resource = rs.getResource(URI.createURI("./model/generated/Protocol.ecore"), true)
for(r:resource.allContents.toIterable.filter(typeof(RoleDec))){
if(r.name == pRole.name){
//DO SOMETHING
}
}
}
In the generator file that I have I already get the Resource object as a parameter.
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
//Generate code
}
How can I do the same thing for my validator. Thank you in advance!
act.eResource() allows to access the resource that contains the action.

How to secure acces to user uploaded files with a filter in Grails?

I am building a Grails application which uses the Spring Security Core Plugin.
I have two applications roles. ROLE_USER and ROLE_ADMIN
Users can upload files which are stored in a directory called files
External users should not see any file
ROLE_ADMIN users can see every uploaded file.
ROLE_USER user should be allowed only in certain cases
A file url request should look like this
http://localhost:8080/MyApp/files/patient1-1.png
For the first case I have set in conf/Config.groovy the next url interceptor
grails.plugins.springsecurity.interceptUrlMap = [
'/files/**': ['ROLE_USER']
]
For the second and third case I created the next file conf/MyFilters
class MyFilters {
def springSecurityService
public currentUser() { return User.get(springSecurityService.principal.id);}
public userRoles() { return springSecurityService.principal.authorities*.authority }
def filters = {
fileFilter(uri: '/files/*') {
before = {
println "Here"
def String url = request.getRequestURL()
if(url.contains("files/patient")) {
if(!userRoles().contains(Role.ROLE_ADMIN)) {
if(PLAIN ROLE USER IS NOT ALLOWED) {
redirect(action:'login')
return false;
}
}
}
}
after = {
}
afterView = {
}
}
}
}
However, it does not seem to get triggered. I never see the Here print out.
Any idea what am I doing wrong?
You shouldn't mix Grails filters with Spring Security - everything is doable from Spring Security. Are you using the "InterceptUrlMap" config type (grails.plugins.springsecurity.securityConfigType = "InterceptUrlMap")? By default it uses annotations, so the securityConfigType setting would be ignored.
If you're using annotations you can add this url pattern to the staticRules config option:
grails.plugins.springsecurity.controllerAnnotations.staticRules = [
'/files/**': ['ROLE_USER']
]
Try running grails clean to force a full compile; it might be a simple as some code being out of sync.

Grails geo - location initializer

We are developing our project in grails. And we want to show data to user according to the country from where they are accessing our website.
I have a field where I store the country location. By using geoip grails plugin.
My question is can we initialize the session with the country name from where the site is being excessed before it hits any controller/action, let say in some config file or somewhere else.
You should be able to do it in a Filter. Something like this, placed in grails-app/conf as GeoFilters.groovy:
class GeoFilters {
def geoIpService
def filters = {
countryCheck(controller:'*', action:'*') {
before = {
if( !session.geolocation ) {
session.geolocation = geoIpService.getLocation( request.remoteAddr )
}
}
}
}
}
Should (I haven't tried it though) check to see if geolocation exists in the session, and if it doesn't, it should fetch it from the geoIpService.

How to get Symfony session variable in model?

How can I pass session variable in symfony model without using sfContext::getInstance()?
The recommended way is called dependency injection, and works like this: you create a setUser() method in your model file, that saves the given parameter to a private property:
class Foo {
private $_user;
public function setUser(myUser $user) {
$this->_user = $user;
}
// ... later:
public function save(Doctrine_Connection $conn = null) {
// use $this->_user to whatever you need
}
}
This looks clumsy, because it is. But without you answering the question what are you trying to do? I cannot give an alternative.
Recommended articles:
What is Dependency Injection? - a post series on Fabien Potencier's blog
Dependency Injection - the design patter in detail on wikipedia
Session variables should be stored as user's attributes.
// in an action:
$this->getUser()->setAttribute('current_order_id', $order_id);
See how to get it back.
// later on, in another action, you can get it as:
$order_id = $this->getUser()->getAttribute('current_order_id', false);
if($order_id!==false)
{
// save to DB
} else {
$this->getUser()->setFlash('error', 'Please selected an order before you can do stuff.');
// redirect and warn the user to selected an order
$this->redirect('orders');
}

Grails facebook graph plugin to check session in every controller

I'm using grails and facebook graph plugin for the user registration. However, instead of checking facebook session in every action and controller. Is there a better way to check the session before entering controller? So, I don't have to duplicate the code to check authentication.
class FacebookSecurityFilters {
def filters = {
all(controller:'*', action:'*') {
before = {
println "test"
}
after = {
}
afterView = {
}
}
}
}
I created this filter by using command grails create-filters . But it's not fired at all, I mean it didn't print "test" at all. Do I need to register the filter? I'm using Grails1.4M01
Thanks
Use a filter - it's a great way to intercept all actions, or a subset of actions based on a pattern: http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.6%20Filters

Resources