You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ol...@apache.org on 2012/04/06 11:59:32 UTC

svn commit: r1310268 [14/42] - in /archiva/redback/redback-core/trunk: ./ redback-authentication/ redback-authentication/redback-authentication-api/ redback-authentication/redback-authentication-api/src/ redback-authentication/redback-authentication-ap...

Added: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultRoleManagementService.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultRoleManagementService.java?rev=1310268&view=auto
==============================================================================
--- archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultRoleManagementService.java (added)
+++ archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultRoleManagementService.java Fri Apr  6 09:58:14 2012
@@ -0,0 +1,893 @@
+package org.codehaus.redback.rest.services;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.plexus.redback.rbac.Permission;
+import org.codehaus.plexus.redback.rbac.RBACManager;
+import org.codehaus.plexus.redback.rbac.RbacManagerException;
+import org.codehaus.plexus.redback.rbac.Resource;
+import org.codehaus.plexus.redback.rbac.UserAssignment;
+import org.codehaus.plexus.redback.role.RoleManager;
+import org.codehaus.plexus.redback.role.RoleManagerException;
+import org.codehaus.plexus.redback.role.model.ModelApplication;
+import org.codehaus.plexus.redback.role.model.ModelRole;
+import org.codehaus.plexus.redback.role.model.ModelTemplate;
+import org.codehaus.plexus.redback.users.User;
+import org.codehaus.plexus.redback.users.UserManager;
+import org.codehaus.plexus.redback.users.UserNotFoundException;
+import org.codehaus.redback.integration.model.AdminEditUserCredentials;
+import org.codehaus.redback.integration.security.role.RedbackRoleConstants;
+import org.codehaus.redback.integration.util.RoleSorter;
+import org.codehaus.redback.rest.api.model.Application;
+import org.codehaus.redback.rest.api.model.ApplicationRoles;
+import org.codehaus.redback.rest.api.model.ErrorMessage;
+import org.codehaus.redback.rest.api.model.Role;
+import org.codehaus.redback.rest.api.model.RoleTemplate;
+import org.codehaus.redback.rest.api.services.RedbackServiceException;
+import org.codehaus.redback.rest.api.services.RoleManagementService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.ws.rs.core.Response;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.3
+ */
+@Service( "roleManagementService#rest" )
+public class DefaultRoleManagementService
+    implements RoleManagementService
+{
+
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    private RoleManager roleManager;
+
+    private RBACManager rbacManager;
+
+    private UserManager userManager;
+
+    @Inject
+    public DefaultRoleManagementService( RoleManager roleManager,
+                                         @Named( value = "rBACManager#cached" ) RBACManager rbacManager,
+                                         @Named( value = "userManager#cached" ) UserManager userManager )
+    {
+        this.roleManager = roleManager;
+        this.rbacManager = rbacManager;
+        this.userManager = userManager;
+    }
+
+    public Boolean createTemplatedRole( String templateId, String resource )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.createTemplatedRole( templateId, resource );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean removeTemplatedRole( String templateId, String resource )
+        throws RedbackServiceException
+    {
+
+        try
+        {
+            roleManager.removeTemplatedRole( templateId, resource );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean updateRole( String templateId, String oldResource, String newResource )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.updateRole( templateId, oldResource, newResource );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean assignRole( String roleId, String principal )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.assignRole( roleId, principal );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean assignRoleByName( String roleName, String principal )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.assignRoleByName( roleName, principal );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean assignTemplatedRole( String templateId, String resource, String principal )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.assignTemplatedRole( templateId, resource, principal );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean unassignRole( String roleId, String principal )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.unassignRole( roleId, principal );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean unassignRoleByName( String roleName, String principal )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.unassignRoleByName( roleName, principal );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean roleExists( String roleId )
+        throws RedbackServiceException
+    {
+        try
+        {
+            return roleManager.roleExists( roleId );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    public Boolean templatedRoleExists( String templateId, String resource )
+        throws RedbackServiceException
+    {
+        try
+        {
+            return roleManager.templatedRoleExists( templateId, resource );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+
+    }
+
+    public Boolean verifyTemplatedRole( String templateId, String resource )
+        throws RedbackServiceException
+    {
+        try
+        {
+            roleManager.verifyTemplatedRole( templateId, resource );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public List<Role> getEffectivelyAssignedRoles( String username )
+        throws RedbackServiceException
+    {
+        if ( StringUtils.isEmpty( username ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "user.cannot.be.null" ) );
+        }
+        try
+        {
+            List<org.codehaus.plexus.redback.rbac.Role> roles =
+                filterAssignableRoles( rbacManager.getEffectivelyAssignedRoles( username ) );
+
+            List<Role> effectivelyAssignedRoles = new ArrayList<Role>( roles.size() );
+
+            for ( org.codehaus.plexus.redback.rbac.Role r : roles )
+            {
+                effectivelyAssignedRoles.add( new Role( r ) );
+            }
+            return effectivelyAssignedRoles;
+        }
+        catch ( RbacManagerException rme )
+        {
+            // ignore, this can happen when the user has no roles assigned  
+        }
+        return new ArrayList<Role>( 0 );
+    }
+
+
+    public List<Application> getApplications( String username )
+        throws RedbackServiceException
+    {
+
+        List<ModelApplication> modelApplications = roleManager.getModel().getApplications();
+
+        List<Application> applications = new ArrayList<Application>( modelApplications.size() );
+
+        for ( ModelApplication modelApplication : modelApplications )
+        {
+            Application application = new Application();
+            application.setDescription( modelApplication.getDescription() );
+            application.setId( modelApplication.getId() );
+            application.setLongDescription( modelApplication.getLongDescription() );
+            application.setVersion( modelApplication.getVersion() );
+            applications.add( application );
+        }
+
+        return applications;
+    }
+
+    public List<Role> getAllRoles()
+        throws RedbackServiceException
+    {
+        try
+        {
+            List<org.codehaus.plexus.redback.rbac.Role> roles = rbacManager.getAllRoles();
+
+            if ( roles == null )
+            {
+                return Collections.emptyList();
+            }
+
+            roles = filterRolesForCurrentUserAccess( roles );
+
+            List<Role> res = new ArrayList<Role>( roles.size() );
+
+            for ( org.codehaus.plexus.redback.rbac.Role r : roles )
+            {
+                res.add( new Role( r ) );
+            }
+            return res;
+
+        }
+        catch ( RbacManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    public List<Role> getDetailedAllRoles()
+        throws RedbackServiceException
+    {
+        try
+        {
+            List<org.codehaus.plexus.redback.rbac.Role> roles = rbacManager.getAllRoles();
+
+            if ( roles == null )
+            {
+                return Collections.emptyList();
+            }
+
+            roles = filterRolesForCurrentUserAccess( roles );
+
+            List<Role> res = new ArrayList<Role>( roles.size() );
+
+            for ( org.codehaus.plexus.redback.rbac.Role r : roles )
+            {
+                res.add( getRole( r.getName() ) );
+            }
+            return res;
+
+        }
+        catch ( RbacManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    private List<org.codehaus.plexus.redback.rbac.Role> filterAssignableRoles(
+        Collection<org.codehaus.plexus.redback.rbac.Role> roles )
+    {
+        List<org.codehaus.plexus.redback.rbac.Role> assignableRoles =
+            new ArrayList<org.codehaus.plexus.redback.rbac.Role>( roles.size() );
+        for ( org.codehaus.plexus.redback.rbac.Role r : roles )
+        {
+            if ( r.isAssignable() )
+            {
+                assignableRoles.add( r );
+            }
+        }
+        return assignableRoles;
+    }
+
+    public Role getRole( String roleName )
+        throws RedbackServiceException
+    {
+        try
+        {
+            org.codehaus.plexus.redback.rbac.Role rbacRole = rbacManager.getRole( roleName );
+            Role role = new Role( rbacRole );
+
+            Map<String, org.codehaus.plexus.redback.rbac.Role> parentRoles = rbacManager.getParentRoles( rbacRole );
+            for ( String parentRoleName : parentRoles.keySet() )
+            {
+                role.getParentRoleNames().add( parentRoleName );
+            }
+
+            List<UserAssignment> userAssignments = rbacManager.getUserAssignmentsForRoles( Arrays.asList( roleName ) );
+
+            if ( userAssignments != null )
+            {
+                for ( UserAssignment userAssignment : userAssignments )
+                {
+                    try
+                    {
+                        User user = userManager.findUser( userAssignment.getPrincipal() );
+                        role.getUsers().add( new org.codehaus.redback.rest.api.model.User( user ) );
+                    }
+                    catch ( UserNotFoundException e )
+                    {
+                        log.warn( "User '" + userAssignment.getPrincipal() + "' doesn't exist.", e );
+                    }
+                }
+            }
+
+            if ( !role.getParentRoleNames().isEmpty() )
+            {
+                List<UserAssignment> userParentAssignments =
+                    rbacManager.getUserAssignmentsForRoles( parentRoles.keySet() );
+                if ( userParentAssignments != null )
+                {
+                    for ( UserAssignment userAssignment : userParentAssignments )
+                    {
+                        try
+                        {
+                            User user = userManager.findUser( userAssignment.getPrincipal() );
+                            role.getParentsRolesUsers().add( new org.codehaus.redback.rest.api.model.User( user ) );
+                        }
+                        catch ( UserNotFoundException e )
+                        {
+                            log.warn( "User '" + userAssignment.getPrincipal() + "' doesn't exist.", e );
+                        }
+                    }
+                }
+            }
+
+            List<org.codehaus.redback.rest.api.model.User> otherUsers =
+                new ArrayList<org.codehaus.redback.rest.api.model.User>();
+            for ( User u : userManager.getUsers() )
+            {
+                org.codehaus.redback.rest.api.model.User user = new org.codehaus.redback.rest.api.model.User( u );
+                if ( role.getParentsRolesUsers().contains( user ) )
+                {
+                    continue;
+                }
+                if ( role.getUsers().contains( user ) )
+                {
+                    continue;
+                }
+                otherUsers.add( user );
+            }
+
+            role.setOtherUsers( otherUsers );
+
+            return role;
+        }
+        catch ( RbacManagerException e )
+        {
+            throw new RedbackServiceException( new ErrorMessage( e.getMessage() ) );
+        }
+    }
+
+    public Boolean updateRoleDescription( String roleName, String description )
+        throws RedbackServiceException
+    {
+        try
+        {
+            org.codehaus.plexus.redback.rbac.Role rbacRole = rbacManager.getRole( roleName );
+            rbacRole.setDescription( description );
+            rbacManager.saveRole( rbacRole );
+        }
+        catch ( RbacManagerException e )
+        {
+            throw new RedbackServiceException( new ErrorMessage( e.getMessage() ) );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean updateRoleUsers( Role role )
+        throws RedbackServiceException
+    {
+
+        for ( org.codehaus.redback.rest.api.model.User user : role.getUsers() )
+        {
+            String username = user.getUsername();
+            if ( !userManager.userExists( username ) )
+            {
+                log.error( "user {} not exits", username );
+                throw new RedbackServiceException( new ErrorMessage( "user.not.exists", new String[]{ username } ) );
+            }
+
+            try
+            {
+                UserAssignment assignment;
+
+                if ( rbacManager.userAssignmentExists( username ) )
+                {
+                    assignment = rbacManager.getUserAssignment( username );
+                }
+                else
+                {
+                    assignment = rbacManager.createUserAssignment( username );
+                }
+
+                assignment.addRoleName( role.getName() );
+                assignment = rbacManager.saveUserAssignment( assignment );
+                log.info( "{} role assigned to {}", role.getName(), username );
+            }
+            catch ( RbacManagerException e )
+            {
+                log.error( "error during assign role " + role.getName() + " to user " + username, e );
+                throw new RedbackServiceException(
+                    new ErrorMessage( "error.assign.role.user", new String[]{ role.getName(), username } ) );
+            }
+        }
+
+        for ( org.codehaus.redback.rest.api.model.User user : role.getRemovedUsers() )
+        {
+            String username = user.getUsername();
+            if ( !userManager.userExists( username ) )
+            {
+                log.error( "user {} not exits", username );
+                throw new RedbackServiceException( new ErrorMessage( "user.not.exists", new String[]{ username } ) );
+            }
+
+            try
+            {
+                UserAssignment assignment;
+
+                if ( rbacManager.userAssignmentExists( username ) )
+                {
+                    assignment = rbacManager.getUserAssignment( username );
+                }
+                else
+                {
+                    assignment = rbacManager.createUserAssignment( username );
+                }
+
+                assignment.removeRoleName( role.getName() );
+                assignment = rbacManager.saveUserAssignment( assignment );
+                log.info( "{} role unassigned to {}", role.getName(), username );
+            }
+            catch ( RbacManagerException e )
+            {
+                log.error( "error during unassign role " + role.getName() + " to user " + username, e );
+                throw new RedbackServiceException(
+                    new ErrorMessage( "error.unassign.role.user", new String[]{ role.getName(), username } ) );
+            }
+        }
+
+        return Boolean.TRUE;
+    }
+
+    public List<ApplicationRoles> getApplicationRoles( String username )
+        throws RedbackServiceException
+    {
+        AdminEditUserCredentials user = null;
+        if ( StringUtils.isEmpty( username ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "rbac.edit.user.empty.principal" ) );
+        }
+
+        if ( !userManager.userExists( username ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "user.does.not.exist", new String[]{ username } ) );
+        }
+
+        try
+        {
+            User u = userManager.findUser( username );
+
+            if ( u == null )
+            {
+                throw new RedbackServiceException( new ErrorMessage( "cannot.operate.on.null.user" ) );
+            }
+
+            user = new AdminEditUserCredentials( u );
+        }
+        catch ( UserNotFoundException e )
+        {
+            throw new RedbackServiceException(
+                new ErrorMessage( "user.does.not.exist", new String[]{ username, e.getMessage() } ) );
+        }
+        try
+        {
+            // check first if role assignments for user exist
+            if ( !rbacManager.userAssignmentExists( username ) )
+            {
+                UserAssignment assignment = rbacManager.createUserAssignment( username );
+                rbacManager.saveUserAssignment( assignment );
+            }
+
+            List<org.codehaus.plexus.redback.rbac.Role> allRoles =
+                filterRolesForCurrentUserAccess( rbacManager.getAllRoles() );
+
+            List<ModelApplication> modelApplications = roleManager.getModel().getApplications();
+
+            List<ApplicationRoles> applicationRolesList = new ArrayList<ApplicationRoles>( modelApplications.size() );
+
+            for ( ModelApplication modelApplication : modelApplications )
+            {
+                ApplicationRoles applicationRoles = new ApplicationRoles();
+
+                applicationRoles.setDescription( modelApplication.getDescription() );
+                applicationRoles.setName( modelApplication.getId() );
+
+                Collection<org.codehaus.plexus.redback.rbac.Role> appRoles =
+                    filterApplicationRoles( modelApplication, allRoles, modelApplication.getTemplates() );
+
+                applicationRoles.setGlobalRoles( toRoleNames( appRoles ) );
+
+                Set<String> resources = discoverResources( modelApplication.getTemplates(), appRoles );
+
+                applicationRoles.setResources( resources );
+
+                applicationRoles.setRoleTemplates( toRoleTemplates( modelApplication.getTemplates() ) );
+
+                // cleanup app roles remove roles coming from templates
+                
+                List<String> appRoleNames = new ArrayList<String>( appRoles.size() );
+                
+                for (String appRoleName : applicationRoles.getGlobalRoles())
+                {
+                    if (!roleFromTemplate( appRoleName, modelApplication.getTemplates() )){
+                        appRoleNames.add( appRoleName );
+                    }
+                }
+                
+                applicationRoles.setGlobalRoles( appRoleNames );
+                
+                applicationRolesList.add( applicationRoles );
+            }
+
+            return applicationRolesList;
+
+        }
+        catch ( RbacManagerException e )
+        {
+            RedbackServiceException redbackServiceException =
+                new RedbackServiceException( new ErrorMessage( e.getMessage() ) );
+            redbackServiceException.setHttpErrorCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
+            throw redbackServiceException;
+        }
+    }
+
+    public Boolean updateUserRoles( org.codehaus.redback.rest.api.model.User user )
+        throws RedbackServiceException
+    {
+
+        String username = user.getUsername();
+
+        if ( StringUtils.isEmpty( username ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "rbac.edit.user.empty.principal" ) );
+        }
+
+        if ( !userManager.userExists( username ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "user.does.not.exist", new String[]{ username } ) );
+        }
+
+        try
+        {
+            User u = userManager.findUser( username );
+
+            if ( u == null )
+            {
+                throw new RedbackServiceException( new ErrorMessage( "cannot.operate.on.null.user" ) );
+            }
+
+        }
+        catch ( UserNotFoundException e )
+        {
+            throw new RedbackServiceException(
+                new ErrorMessage( "user.does.not.exist", new String[]{ username, e.getMessage() } ) );
+        }
+
+        try
+        {
+
+            UserAssignment assignment;
+
+            if ( rbacManager.userAssignmentExists( username ) )
+            {
+                assignment = rbacManager.getUserAssignment( username );
+            }
+            else
+            {
+                assignment = rbacManager.createUserAssignment( username );
+            }
+
+            assignment.setRoleNames( user.getAssignedRoles() );
+
+            assignment = rbacManager.saveUserAssignment( assignment );
+
+        }
+        catch ( RbacManagerException e )
+        {
+            RedbackServiceException redbackServiceException =
+                new RedbackServiceException( new ErrorMessage( e.getMessage() ) );
+            redbackServiceException.setHttpErrorCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
+            throw redbackServiceException;
+        }
+
+        return Boolean.TRUE;
+
+    }
+
+    //----------------------------------------------------------------
+    // Internal methods
+    //----------------------------------------------------------------
+
+    private org.codehaus.plexus.redback.rbac.Role isInList( String roleName,
+                                                            Collection<org.codehaus.plexus.redback.rbac.Role> roles )
+    {
+        for ( org.codehaus.plexus.redback.rbac.Role role : roles )
+        {
+            if ( roleName.equals( role.getName() ) )
+            {
+                return role;
+            }
+        }
+        return null;
+    }
+
+    private Collection<org.codehaus.plexus.redback.rbac.Role> filterApplicationRoles( ModelApplication application,
+                                                                                      List<org.codehaus.plexus.redback.rbac.Role> allRoles,
+                                                                                      List<ModelTemplate> applicationTemplates )
+    {
+        Set<org.codehaus.plexus.redback.rbac.Role> applicationRoles =
+            new HashSet<org.codehaus.plexus.redback.rbac.Role>();
+        List<ModelRole> roles = application.getRoles();
+
+        for ( ModelRole modelRole : roles )
+        {
+            org.codehaus.plexus.redback.rbac.Role r = isInList( modelRole.getName(), allRoles );
+            if ( r != null )
+            {
+                applicationRoles.add( r );
+            }
+        }
+
+        List<String> roleNames = toRoleNames( allRoles );
+
+        for ( ModelTemplate modelTemplate : applicationTemplates )
+        {
+            for ( org.codehaus.plexus.redback.rbac.Role r : allRoles )
+            {
+                if ( StringUtils.startsWith( r.getName(),
+                                             modelTemplate.getNamePrefix() + modelTemplate.getDelimiter() ) )
+                {
+                    applicationRoles.add( r );
+                }
+            }
+        }
+
+        return applicationRoles;
+    }
+
+    private boolean roleFromTemplate( String roleName, List<ModelTemplate> applicationTemplates )
+    {
+
+        for ( ModelTemplate modelTemplate : applicationTemplates )
+        {
+            if ( StringUtils.startsWith( roleName, modelTemplate.getNamePrefix() + modelTemplate.getDelimiter() ) )
+            {
+                return true;
+            }
+
+        }
+        return false;
+    }
+
+    private List<String> toRoleNames( Collection<org.codehaus.plexus.redback.rbac.Role> roles )
+    {
+        List<String> names = new ArrayList<String>( roles.size() );
+
+        for ( org.codehaus.plexus.redback.rbac.Role r : roles )
+        {
+            names.add( r.getName() );
+        }
+
+        return names;
+    }
+
+    private List<RoleTemplate> toRoleTemplates( List<ModelTemplate> modelTemplates )
+    {
+        if ( modelTemplates == null || modelTemplates.isEmpty() )
+        {
+            return new ArrayList<RoleTemplate>( 0 );
+        }
+
+        List<RoleTemplate> roleTemplates = new ArrayList<RoleTemplate>( modelTemplates.size() );
+
+        for ( ModelTemplate modelTemplate : modelTemplates )
+        {
+            RoleTemplate roleTemplate = new RoleTemplate();
+
+            roleTemplate.setDelimiter( modelTemplate.getDelimiter() );
+            roleTemplate.setDescription( modelTemplate.getDescription() );
+            roleTemplate.setId( modelTemplate.getId() );
+            roleTemplate.setNamePrefix( modelTemplate.getNamePrefix() );
+
+            roleTemplates.add( roleTemplate );
+        }
+
+        return roleTemplates;
+    }
+
+    private Set<String> discoverResources( List<ModelTemplate> applicationTemplates,
+                                           Collection<org.codehaus.plexus.redback.rbac.Role> roles )
+    {
+        Set<String> resources = new HashSet<String>();
+        for ( ModelTemplate modelTemplate : applicationTemplates )
+        {
+            for ( org.codehaus.plexus.redback.rbac.Role role : roles )
+            {
+                String roleName = role.getName();
+                if ( roleName.startsWith( modelTemplate.getNamePrefix() ) )
+                {
+                    String delimiter = modelTemplate.getDelimiter();
+                    resources.add( roleName.substring( roleName.indexOf( delimiter ) + delimiter.length() ) );
+                }
+            }
+        }
+        return resources;
+    }
+
+    /**
+     * this is a hack. this is a hack around the requirements of putting RBAC constraints into the model. this adds one
+     * very major restriction to this security system, that a role name must contain the identifiers of the resource
+     * that is being constrained for adding and granting of roles, this is unacceptable in the long term and we need to
+     * get the model refactored to include this RBAC concept
+     *
+     * @param roleList
+     * @return
+     * @throws org.codehaus.plexus.redback.rbac.RbacManagerException
+     *
+     */
+    protected List<org.codehaus.plexus.redback.rbac.Role> filterRolesForCurrentUserAccess(
+        List<org.codehaus.plexus.redback.rbac.Role> roleList )
+        throws RedbackServiceException
+    {
+        RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
+        // olamy: should not happened normally as annotations check this first
+        if ( redbackRequestInformation == null || redbackRequestInformation.getUser() == null )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "login.mandatory" ) );
+        }
+        String currentUser = redbackRequestInformation.getUser().getUsername();
+
+        List<org.codehaus.plexus.redback.rbac.Role> filteredRoleList =
+            new ArrayList<org.codehaus.plexus.redback.rbac.Role>();
+        try
+        {
+            Map<String, List<Permission>> assignedPermissionMap = rbacManager.getAssignedPermissionMap( currentUser );
+            List<String> resourceGrants = new ArrayList<String>();
+
+            if ( assignedPermissionMap.containsKey( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION ) )
+            {
+                List<Permission> roleGrantPermissions =
+                    assignedPermissionMap.get( RedbackRoleConstants.USER_MANAGEMENT_ROLE_GRANT_OPERATION );
+
+                for ( Permission permission : roleGrantPermissions )
+                {
+                    if ( permission.getResource().getIdentifier().equals( Resource.GLOBAL ) )
+                    {
+                        // the current user has the rights to assign any given role
+                        return roleList;
+                    }
+                    else
+                    {
+                        resourceGrants.add( permission.getResource().getIdentifier() );
+                    }
+                }
+
+            }
+            else
+            {
+                return Collections.emptyList();
+            }
+
+            String delimiter = " - ";
+
+            // we should have a list of resourceGrants now, this will provide us with the information necessary to restrict
+            // the role list
+            for ( org.codehaus.plexus.redback.rbac.Role role : roleList )
+            {
+                int delimiterIndex = role.getName().indexOf( delimiter );
+                for ( String resourceIdentifier : resourceGrants )
+                {
+
+                    if ( ( role.getName().indexOf( resourceIdentifier ) != -1 ) && ( delimiterIndex != -1 ) )
+                    {
+                        String resourceName = role.getName().substring( delimiterIndex + delimiter.length() );
+                        if ( resourceName.equals( resourceIdentifier ) )
+                        {
+                            filteredRoleList.add( role );
+                        }
+                    }
+                }
+            }
+        }
+        catch ( RbacManagerException rme )
+        {
+            // ignore, this can happen when the user has no roles assigned  
+        }
+        Collections.sort( filteredRoleList, new RoleSorter() );
+        return filteredRoleList;
+    }
+
+
+}

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultRoleManagementService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultRoleManagementService.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUserService.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUserService.java?rev=1310268&view=auto
==============================================================================
--- archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUserService.java (added)
+++ archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUserService.java Fri Apr  6 09:58:14 2012
@@ -0,0 +1,906 @@
+package org.codehaus.redback.rest.services;
+
+/*
+ * Copyright 2011 The Codehaus.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import net.sf.ehcache.CacheManager;
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.plexus.cache.Cache;
+import org.codehaus.plexus.redback.authentication.AuthenticationException;
+import org.codehaus.plexus.redback.authentication.TokenBasedAuthenticationDataSource;
+import org.codehaus.plexus.redback.configuration.UserConfiguration;
+import org.codehaus.plexus.redback.keys.AuthenticationKey;
+import org.codehaus.plexus.redback.keys.KeyManager;
+import org.codehaus.plexus.redback.keys.KeyManagerException;
+import org.codehaus.plexus.redback.keys.KeyNotFoundException;
+import org.codehaus.plexus.redback.policy.AccountLockedException;
+import org.codehaus.plexus.redback.policy.MustChangePasswordException;
+import org.codehaus.plexus.redback.policy.PasswordEncoder;
+import org.codehaus.plexus.redback.policy.UserSecurityPolicy;
+import org.codehaus.plexus.redback.rbac.RBACManager;
+import org.codehaus.plexus.redback.rbac.RbacManagerException;
+import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
+import org.codehaus.plexus.redback.rbac.UserAssignment;
+import org.codehaus.plexus.redback.role.RoleManager;
+import org.codehaus.plexus.redback.role.RoleManagerException;
+import org.codehaus.plexus.redback.system.SecuritySystem;
+import org.codehaus.plexus.redback.users.UserManager;
+import org.codehaus.plexus.redback.users.UserNotFoundException;
+import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
+import org.codehaus.redback.integration.mail.Mailer;
+import org.codehaus.redback.integration.security.role.RedbackRoleConstants;
+import org.codehaus.redback.rest.api.model.ErrorMessage;
+import org.codehaus.redback.rest.api.model.Operation;
+import org.codehaus.redback.rest.api.model.Permission;
+import org.codehaus.redback.rest.api.model.RegistrationKey;
+import org.codehaus.redback.rest.api.model.Resource;
+import org.codehaus.redback.rest.api.model.User;
+import org.codehaus.redback.rest.api.services.RedbackServiceException;
+import org.codehaus.redback.rest.api.services.UserService;
+import org.codehaus.redback.rest.services.utils.PasswordValidator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+@Service( "userService#rest" )
+public class DefaultUserService
+    implements UserService
+{
+
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    private static final String VALID_USERNAME_CHARS = "[a-zA-Z_0-9\\-.@]*";
+
+    private UserManager userManager;
+
+    private SecuritySystem securitySystem;
+
+    @Inject
+    private UserConfiguration config;
+
+    @Inject
+    private RoleManager roleManager;
+
+    /**
+     * cache used for user assignments
+     */
+    @Inject
+    @Named( value = "cache#userAssignments" )
+    private Cache userAssignmentsCache;
+
+    /**
+     * cache used for user permissions
+     */
+    @Inject
+    @Named( value = "cache#userPermissions" )
+    private Cache userPermissionsCache;
+
+    /**
+     * Cache used for users
+     */
+    @Inject
+    @Named( value = "cache#users" )
+    private Cache usersCache;
+
+    @Inject
+    private Mailer mailer;
+
+    @Inject
+    @Named( value = "rBACManager#cached" )
+    private RBACManager rbacManager;
+
+    private HttpAuthenticator httpAuthenticator;
+
+    @Inject
+    private PasswordValidator passwordValidator;
+
+    @Context
+    private HttpServletRequest httpServletRequest;
+
+    @Inject
+    public DefaultUserService( @Named( value = "userManager#cached" ) UserManager userManager,
+                               SecuritySystem securitySystem,
+                               @Named( "httpAuthenticator#basic" ) HttpAuthenticator httpAuthenticator )
+    {
+        this.userManager = userManager;
+        this.securitySystem = securitySystem;
+        this.httpAuthenticator = httpAuthenticator;
+    }
+
+
+    public Boolean createUser( User user )
+        throws RedbackServiceException
+    {
+
+        try
+        {
+            org.codehaus.plexus.redback.users.User u = userManager.findUser( user.getUsername() );
+            if ( u != null )
+            {
+                throw new RedbackServiceException(
+                    new ErrorMessage( "user " + user.getUsername() + " already exists" ) );
+            }
+        }
+        catch ( UserNotFoundException e )
+        {
+            //ignore we just want to prevent non human readable error message from backend :-)
+            log.debug( "user {} not exists", user.getUsername() );
+        }
+
+        // data validation
+        if ( StringUtils.isEmpty( user.getUsername() ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "username cannot be empty" ) );
+        }
+
+        if ( StringUtils.isEmpty( user.getFullName() ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "fullName cannot be empty" ) );
+        }
+
+        if ( StringUtils.isEmpty( user.getEmail() ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "email cannot be empty" ) );
+        }
+
+        org.codehaus.plexus.redback.users.User u =
+            userManager.createUser( user.getUsername(), user.getFullName(), user.getEmail() );
+        u.setPassword( user.getPassword() );
+        u.setLocked( user.isLocked() );
+        u.setPasswordChangeRequired( user.isPasswordChangeRequired() );
+        u.setPermanent( user.isPermanent() );
+        u.setValidated( user.isValidated() );
+        u = userManager.addUser( u );
+        if ( !user.isPasswordChangeRequired() )
+        {
+            u.setPasswordChangeRequired( false );
+            try
+            {
+                u = userManager.updateUser( u );
+                log.debug( "user {} created", u.getUsername() );
+            }
+            catch ( UserNotFoundException e )
+            {
+                throw new RedbackServiceException( e.getMessage() );
+            }
+        }
+        try
+        {
+            roleManager.assignRole( RedbackRoleConstants.REGISTERED_USER_ROLE_ID, u.getPrincipal().toString() );
+        }
+        catch ( RoleManagerException rpe )
+        {
+            log.error( "RoleProfile Error: " + rpe.getMessage(), rpe );
+            throw new RedbackServiceException( new ErrorMessage( "assign.role.failure", null ) );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean deleteUser( String username )
+        throws RedbackServiceException
+    {
+
+        try
+        {
+
+            if ( rbacManager.userAssignmentExists( username ) )
+            {
+                UserAssignment assignment = rbacManager.getUserAssignment( username );
+                rbacManager.removeUserAssignment( assignment );
+            }
+
+        }
+        catch ( RbacManagerException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        try
+        {
+            userManager.deleteUser( username );
+            return Boolean.TRUE;
+        }
+        catch ( UserNotFoundException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        finally
+        {
+            removeFromCache( username );
+        }
+    }
+
+
+    public User getUser( String username )
+        throws RedbackServiceException
+    {
+        try
+        {
+            org.codehaus.plexus.redback.users.User user = userManager.findUser( username );
+            return getSimpleUser( user );
+        }
+        catch ( UserNotFoundException e )
+        {
+            return null;
+        }
+    }
+
+    public List<User> getUsers()
+        throws RedbackServiceException
+    {
+        List<org.codehaus.plexus.redback.users.User> users = userManager.getUsers();
+        List<User> simpleUsers = new ArrayList<User>( users.size() );
+
+        for ( org.codehaus.plexus.redback.users.User user : users )
+        {
+            simpleUsers.add( getSimpleUser( user ) );
+        }
+
+        return simpleUsers;
+    }
+
+    public Boolean updateMe( User user )
+        throws RedbackServiceException
+    {
+        // check username == one in the session
+        RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
+        if ( redbackRequestInformation == null || redbackRequestInformation.getUser() == null )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "you must be logged to update your profile" ),
+                                               Response.Status.FORBIDDEN.getStatusCode() );
+        }
+        if ( user == null )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "user parameter is mandatory" ),
+                                               Response.Status.BAD_REQUEST.getStatusCode() );
+        }
+        if ( !StringUtils.equals( redbackRequestInformation.getUser().getUsername(), user.getUsername() ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "you can update only your profile" ),
+                                               Response.Status.FORBIDDEN.getStatusCode() );
+        }
+
+        if ( StringUtils.isEmpty( user.getPreviousPassword() ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "previous password is empty" ),
+                                               Response.Status.BAD_REQUEST.getStatusCode() );
+        }
+
+        User realUser = getUser( user.getUsername() );
+        try
+        {
+            String previousEncodedPassword =
+                securitySystem.getUserManager().findUser( user.getUsername() ).getEncodedPassword();
+
+            // check oldPassword with the current one
+
+            PasswordEncoder encoder = securitySystem.getPolicy().getPasswordEncoder();
+
+            if ( !encoder.isPasswordValid( previousEncodedPassword, user.getPreviousPassword() ) )
+            {
+
+                throw new RedbackServiceException( new ErrorMessage( "password.provided.does.not.match.existing" ),
+                                                   Response.Status.BAD_REQUEST.getStatusCode() );
+            }
+        }
+        catch ( UserNotFoundException e )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "user not found" ),
+                                               Response.Status.BAD_REQUEST.getStatusCode() );
+        }
+        // only 3 fields to update
+        realUser.setFullName( user.getFullName() );
+        realUser.setEmail( user.getEmail() );
+        // ui can limit to not update password
+        if ( StringUtils.isNotBlank( user.getPassword() ) )
+        {
+            passwordValidator.validatePassword( user.getPassword(), user.getUsername() );
+
+            realUser.setPassword( user.getPassword() );
+        }
+
+        updateUser( realUser );
+
+        return Boolean.TRUE;
+    }
+
+    public Boolean updateUser( User user )
+        throws RedbackServiceException
+    {
+        try
+        {
+            org.codehaus.plexus.redback.users.User rawUser = userManager.findUser( user.getUsername() );
+            rawUser.setFullName( user.getFullName() );
+            rawUser.setEmail( user.getEmail() );
+            rawUser.setValidated( user.isValidated() );
+            rawUser.setLocked( user.isLocked() );
+            rawUser.setPassword( user.getPassword() );
+            rawUser.setPasswordChangeRequired( user.isPasswordChangeRequired() );
+            rawUser.setPermanent( user.isPermanent() );
+
+            userManager.updateUser( rawUser );
+            return Boolean.TRUE;
+        }
+        catch ( UserNotFoundException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    public int removeFromCache( String userName )
+        throws RedbackServiceException
+    {
+        if ( userAssignmentsCache != null )
+        {
+            userAssignmentsCache.remove( userName );
+        }
+        if ( userPermissionsCache != null )
+        {
+            userPermissionsCache.remove( userName );
+        }
+        if ( usersCache != null )
+        {
+            usersCache.remove( userName );
+        }
+
+        CacheManager cacheManager = CacheManager.getInstance();
+        String[] caches = cacheManager.getCacheNames();
+        for ( String cacheName : caches )
+        {
+            if ( StringUtils.startsWith( cacheName, "org.codehaus.plexus.redback.rbac.jdo" ) )
+            {
+                cacheManager.getCache( cacheName ).removeAll();
+            }
+        }
+
+        return 0;
+    }
+
+    public User getGuestUser()
+        throws RedbackServiceException
+    {
+        try
+        {
+            org.codehaus.plexus.redback.users.User user = userManager.getGuestUser();
+            return getSimpleUser( user );
+        }
+        catch ( Exception e )
+        {
+            return null;
+        }
+    }
+
+    public User createGuestUser()
+        throws RedbackServiceException
+    {
+        User u = getGuestUser();
+        if ( u != null )
+        {
+            return u;
+        }
+        // temporary disable policy during guest creation as no password !
+        try
+        {
+            securitySystem.getPolicy().setEnabled( false );
+            org.codehaus.plexus.redback.users.User user = userManager.createGuestUser();
+            user.setPasswordChangeRequired( false );
+            user = userManager.updateUser( user, false );
+            roleManager.assignRole( "guest", user.getPrincipal().toString() );
+            return getSimpleUser( user );
+        }
+        catch ( RoleManagerException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        catch ( UserNotFoundException e )
+        {
+            // olamy I wonder how this can happen :-)
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        finally
+        {
+
+            if ( !securitySystem.getPolicy().isEnabled() )
+            {
+                securitySystem.getPolicy().setEnabled( true );
+            }
+        }
+    }
+
+    public Boolean ping()
+        throws RedbackServiceException
+    {
+        return Boolean.TRUE;
+    }
+
+    private User getSimpleUser( org.codehaus.plexus.redback.users.User user )
+    {
+        if ( user == null )
+        {
+            return null;
+        }
+        return new User( user );
+    }
+
+    public Boolean createAdminUser( User adminUser )
+        throws RedbackServiceException
+    {
+        if ( isAdminUserExists() )
+        {
+            return Boolean.FALSE;
+        }
+
+        org.codehaus.plexus.redback.users.User user =
+            userManager.createUser( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminUser.getFullName(),
+                                    adminUser.getEmail() );
+        user.setPassword( adminUser.getPassword() );
+
+        user.setLocked( false );
+        user.setPasswordChangeRequired( false );
+        user.setPermanent( true );
+        user.setValidated( true );
+
+        userManager.addUser( user );
+
+        try
+        {
+            roleManager.assignRole( "system-administrator", user.getPrincipal().toString() );
+        }
+        catch ( RoleManagerException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        return Boolean.TRUE;
+    }
+
+    public Boolean isAdminUserExists()
+        throws RedbackServiceException
+    {
+        try
+        {
+            userManager.findUser( config.getString( "redback.default.admin" ) );
+            return Boolean.TRUE;
+        }
+        catch ( UserNotFoundException e )
+        {
+            // ignore
+        }
+        return Boolean.FALSE;
+    }
+
+    public Boolean resetPassword( String username )
+        throws RedbackServiceException
+    {
+        if ( StringUtils.isEmpty( username ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "username.cannot.be.empty" ) );
+        }
+
+        UserManager userManager = securitySystem.getUserManager();
+        KeyManager keyManager = securitySystem.getKeyManager();
+        UserSecurityPolicy policy = securitySystem.getPolicy();
+
+        try
+        {
+            org.codehaus.plexus.redback.users.User user = userManager.findUser( username );
+
+            AuthenticationKey authkey = keyManager.createKey( username, "Password Reset Request",
+                                                              policy.getUserValidationSettings().getEmailValidationTimeout() );
+
+            mailer.sendPasswordResetEmail( Arrays.asList( user.getEmail() ), authkey, getBaseUrl() );
+
+            log.info( "password reset request for username {}", username );
+        }
+        catch ( UserNotFoundException e )
+        {
+            log.info( "Password Reset on non-existant user [{}].", username );
+            throw new RedbackServiceException( new ErrorMessage( "password.reset.failure" ) );
+        }
+        catch ( KeyManagerException e )
+        {
+            log.info( "Unable to issue password reset.", e );
+            throw new RedbackServiceException( new ErrorMessage( "password.reset.email.generation.failure" ) );
+        }
+
+        return Boolean.TRUE;
+    }
+
+    public RegistrationKey registerUser( User user )
+        throws RedbackServiceException
+    {
+        if ( user == null )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "invalid.user.credentials", null ) );
+
+        }
+
+        UserSecurityPolicy securityPolicy = securitySystem.getPolicy();
+
+        boolean emailValidationRequired = securityPolicy.getUserValidationSettings().isEmailValidationRequired();
+
+        if ( emailValidationRequired )
+        {
+            validateCredentialsLoose( user );
+        }
+        else
+        {
+            validateCredentialsStrict( user );
+        }
+
+        // NOTE: Do not perform Password Rules Validation Here.
+
+        if ( userManager.userExists( user.getUsername() ) )
+        {
+            throw new RedbackServiceException(
+                new ErrorMessage( "user.already.exists", new String[]{ user.getUsername() } ) );
+        }
+
+        org.codehaus.plexus.redback.users.User u =
+            userManager.createUser( user.getUsername(), user.getFullName(), user.getEmail() );
+        u.setPassword( user.getPassword() );
+        u.setValidated( false );
+        u.setLocked( false );
+
+        try
+        {
+            roleManager.assignRole( RedbackRoleConstants.REGISTERED_USER_ROLE_ID, u.getPrincipal().toString() );
+        }
+        catch ( RoleManagerException rpe )
+        {
+            log.error( "RoleProfile Error: " + rpe.getMessage(), rpe );
+            throw new RedbackServiceException( new ErrorMessage( "assign.role.failure", null ) );
+        }
+
+        if ( emailValidationRequired )
+        {
+            u.setLocked( true );
+
+            try
+            {
+                AuthenticationKey authkey =
+                    securitySystem.getKeyManager().createKey( u.getPrincipal().toString(), "New User Email Validation",
+                                                              securityPolicy.getUserValidationSettings().getEmailValidationTimeout() );
+
+                mailer.sendAccountValidationEmail( Arrays.asList( u.getEmail() ), authkey, getBaseUrl() );
+
+                securityPolicy.setEnabled( false );
+                userManager.addUser( u );
+                return new RegistrationKey( authkey.getKey() );
+
+            }
+            catch ( KeyManagerException e )
+            {
+                log.error( "Unable to register a new user.", e );
+                throw new RedbackServiceException( new ErrorMessage( "cannot.register.user", null ) );
+            }
+            finally
+            {
+                securityPolicy.setEnabled( true );
+            }
+        }
+        else
+        {
+            userManager.addUser( u );
+            return new RegistrationKey( "-1" );
+        }
+
+        // FIXME log this event
+        /*
+        AuditEvent event = new AuditEvent( getText( "log.account.create" ) );
+        event.setAffectedUser( username );
+        event.log();
+        */
+
+    }
+
+    public Boolean validateUserFromKey( String key )
+        throws RedbackServiceException
+    {
+        String principal = null;
+        try
+        {
+            AuthenticationKey authkey = securitySystem.getKeyManager().findKey( key );
+
+            org.codehaus.plexus.redback.users.User user =
+                securitySystem.getUserManager().findUser( authkey.getForPrincipal() );
+
+            user.setValidated( true );
+            user.setLocked( false );
+            user.setPasswordChangeRequired( true );
+            user.setEncodedPassword( "" );
+
+            principal = user.getPrincipal().toString();
+
+            TokenBasedAuthenticationDataSource authsource = new TokenBasedAuthenticationDataSource();
+            authsource.setPrincipal( principal );
+            authsource.setToken( authkey.getKey() );
+            authsource.setEnforcePasswordChange( false );
+
+            securitySystem.getUserManager().updateUser( user );
+
+            httpAuthenticator.authenticate( authsource, httpServletRequest.getSession( true ) );
+
+            log.info( "account validated for user {}", user.getUsername() );
+
+            return Boolean.TRUE;
+        }
+        catch ( MustChangePasswordException e )
+        {
+            throw new RedbackServiceException( e.getMessage(), Response.Status.FORBIDDEN.getStatusCode() );
+        }
+        catch ( KeyNotFoundException e )
+        {
+            log.info( "Invalid key requested: {}", key );
+            throw new RedbackServiceException( new ErrorMessage( "cannot.find.key" ) );
+        }
+        catch ( KeyManagerException e )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "cannot.find.key.at.the.momment" ) );
+
+        }
+        catch ( UserNotFoundException e )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "cannot.find.user", new String[]{ principal } ) );
+
+        }
+        catch ( AccountLockedException e )
+        {
+            throw new RedbackServiceException( e.getMessage(), Response.Status.FORBIDDEN.getStatusCode() );
+        }
+        catch ( AuthenticationException e )
+        {
+            throw new RedbackServiceException( e.getMessage(), Response.Status.FORBIDDEN.getStatusCode() );
+        }
+    }
+
+    public Collection<Permission> getCurrentUserPermissions()
+        throws RedbackServiceException
+    {
+        RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
+        String userName = UserManager.GUEST_USERNAME;
+        if ( redbackRequestInformation != null && redbackRequestInformation.getUser() != null )
+        {
+            userName = redbackRequestInformation.getUser().getUsername();
+        }
+
+        return getUserPermissions( userName );
+    }
+
+    public Collection<Operation> getCurrentUserOperations()
+        throws RedbackServiceException
+    {
+        RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
+        String userName = UserManager.GUEST_USERNAME;
+        if ( redbackRequestInformation != null && redbackRequestInformation.getUser() != null )
+        {
+            userName = redbackRequestInformation.getUser().getUsername();
+        }
+
+        return getUserOperations( userName );
+    }
+
+    public Collection<Operation> getUserOperations( String userName )
+        throws RedbackServiceException
+    {
+        Collection<Permission> permissions = getUserPermissions( userName );
+        List<Operation> operations = new ArrayList<Operation>( permissions.size() );
+        for ( Permission permission : permissions )
+        {
+            if ( permission.getOperation() != null )
+            {
+                Operation operation = new Operation();
+                operation.setName( permission.getOperation().getName() );
+                operations.add( operation );
+            }
+        }
+        return operations;
+    }
+
+    public Collection<Permission> getUserPermissions( String userName )
+        throws RedbackServiceException
+    {
+        try
+        {
+            Set<org.codehaus.plexus.redback.rbac.Permission> permissions =
+                rbacManager.getAssignedPermissions( userName );
+            // FIXME return guest permissions !!
+            List<Permission> userPermissions = new ArrayList<Permission>( permissions.size() );
+            for ( org.codehaus.plexus.redback.rbac.Permission p : permissions )
+            {
+                Permission permission = new Permission();
+                permission.setName( p.getName() );
+
+                if ( p.getOperation() != null )
+                {
+                    Operation operation = new Operation();
+                    operation.setName( p.getOperation().getName() );
+                    permission.setOperation( operation );
+                }
+
+                if ( p.getResource() != null )
+                {
+                    Resource resource = new Resource();
+                    resource.setIdentifier( p.getResource().getIdentifier() );
+                    resource.setPattern( p.getResource().isPattern() );
+                    permission.setResource( resource );
+                }
+
+                userPermissions.add( permission );
+            }
+            return userPermissions;
+        }
+        catch ( RbacObjectNotFoundException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+        catch ( RbacManagerException e )
+        {
+            log.error( e.getMessage(), e );
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    public void validateCredentialsLoose( User user )
+        throws RedbackServiceException
+    {
+        RedbackServiceException redbackServiceException =
+            new RedbackServiceException( "issues during validating user" );
+        if ( org.codehaus.plexus.util.StringUtils.isEmpty( user.getUsername() ) )
+        {
+            redbackServiceException.addErrorMessage( new ErrorMessage( "username.required", null ) );
+        }
+        else
+        {
+            if ( !user.getUsername().matches( VALID_USERNAME_CHARS ) )
+            {
+                redbackServiceException.addErrorMessage( new ErrorMessage( "username.invalid.characters", null ) );
+            }
+        }
+
+        if ( org.codehaus.plexus.util.StringUtils.isEmpty( user.getFullName() ) )
+        {
+            redbackServiceException.addErrorMessage( new ErrorMessage( "fullName.required", null ) );
+        }
+
+        if ( org.codehaus.plexus.util.StringUtils.isEmpty( user.getEmail() ) )
+        {
+            redbackServiceException.addErrorMessage( new ErrorMessage( "email.required", null ) );
+        }
+
+        if ( !org.codehaus.plexus.util.StringUtils.equals( user.getPassword(), user.getConfirmPassword() ) )
+        {
+            redbackServiceException.addErrorMessage( new ErrorMessage( "passwords.does.not.match", null ) );
+        }
+
+        try
+        {
+            if ( !org.codehaus.plexus.util.StringUtils.isEmpty( user.getEmail() ) )
+            {
+                new InternetAddress( user.getEmail(), true );
+            }
+        }
+        catch ( AddressException e )
+        {
+            redbackServiceException.addErrorMessage( new ErrorMessage( "email.invalid", null ) );
+        }
+        if ( !redbackServiceException.getErrorMessages().isEmpty() )
+        {
+            throw redbackServiceException;
+        }
+    }
+
+    public void validateCredentialsStrict( User user )
+        throws RedbackServiceException
+    {
+        validateCredentialsLoose( user );
+
+        org.codehaus.plexus.redback.users.User tmpuser =
+            userManager.createUser( user.getUsername(), user.getFullName(), user.getEmail() );
+
+        user.setPassword( user.getPassword() );
+
+        securitySystem.getPolicy().validatePassword( tmpuser );
+
+        if ( ( org.codehaus.plexus.util.StringUtils.isEmpty( user.getPassword() ) ) )
+        {
+            throw new RedbackServiceException( new ErrorMessage( "password.required", null ) );
+        }
+    }
+
+    private String getBaseUrl()
+    {
+        if ( httpServletRequest != null )
+        {
+            if ( httpServletRequest != null )
+            {
+                return httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + (
+                    httpServletRequest.getServerPort() == 80
+                        ? ""
+                        : ":" + httpServletRequest.getServerPort() ) + httpServletRequest.getContextPath();
+            }
+        }
+        return null;
+    }
+
+    public Boolean unlockUser( String username )
+        throws RedbackServiceException
+    {
+        User user = getUser( username );
+        if ( user == null )
+        {
+            user.setLocked( false );
+            updateUser( user );
+            return Boolean.TRUE;
+        }
+        return Boolean.FALSE;
+    }
+
+    public Boolean lockUser( String username )
+        throws RedbackServiceException
+    {
+        User user = getUser( username );
+        if ( user == null )
+        {
+            user.setLocked( true );
+            updateUser( user );
+            return Boolean.TRUE;
+        }
+        return Boolean.FALSE;
+    }
+
+    public Boolean passwordChangeRequired( String username )
+        throws RedbackServiceException
+    {
+        User user = getUser( username );
+        if ( user == null )
+        {
+            user.setPasswordChangeRequired( true );
+            updateUser( user );
+            return Boolean.TRUE;
+        }
+        return Boolean.FALSE;
+    }
+
+    public Boolean passwordChangeNotRequired( String username )
+        throws RedbackServiceException
+    {
+        User user = getUser( username );
+        if ( user == null )
+        {
+            user.setPasswordChangeRequired( false );
+            updateUser( user );
+            return Boolean.TRUE;
+        }
+        return Boolean.FALSE;
+    }
+}

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUserService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUserService.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUtilServices.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUtilServices.java?rev=1310268&view=auto
==============================================================================
--- archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUtilServices.java (added)
+++ archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUtilServices.java Fri Apr  6 09:58:14 2012
@@ -0,0 +1,164 @@
+package org.codehaus.redback.rest.services;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.redback.rest.api.services.RedbackServiceException;
+import org.codehaus.redback.rest.api.services.UtilServices;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+@Service( "utilServices#rest" )
+public class DefaultUtilServices
+    implements UtilServices
+{
+
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
+
+    @PostConstruct
+    public void init()
+        throws RedbackServiceException
+    {
+
+        // preload i18n en and fr
+        getI18nProperties( "en" );
+        getI18nProperties( "fr" );
+    }
+
+    public String getI18nResources( String locale )
+        throws RedbackServiceException
+    {
+        String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
+        if ( cachedi18n != null )
+        {
+            return cachedi18n;
+        }
+
+        Properties properties = new Properties();
+
+        // load redback user api messages
+        try
+        {
+
+            // load default first then requested locale
+            loadResource( properties, "org/codehaus/plexus/redback/users/messages", null );
+            loadResource( properties, "org/codehaus/plexus/redback/users/messages", locale );
+
+        }
+        catch ( IOException e )
+        {
+            log.warn( "skip error loading properties {}", "org/codehaus/plexus/redback/users/messages" );
+        }
+
+        try
+        {
+
+            // load default first then requested locale
+            loadResource( properties, "org/codehaus/redback/i18n/default", null );
+            loadResource( properties, "org/codehaus/redback/i18n/default", locale );
+
+        }
+        catch ( IOException e )
+        {
+            log.warn( "skip error loading properties {}", "org/codehaus/redback/i18n/default" );
+        }
+
+        StringBuilder output = new StringBuilder();
+
+        for ( Map.Entry<Object, Object> entry : properties.entrySet() )
+        {
+            output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
+            output.append( '\n' );
+        }
+
+        cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), output.toString() );
+
+        return output.toString();
+    }
+
+    public Properties getI18nProperties( String locale )
+        throws RedbackServiceException
+    {
+        try
+        {
+            Properties properties = new Properties();
+            // load default first then requested locale
+            loadResource( properties, "org/codehaus/plexus/redback/users/messages", null );
+            loadResource( properties, "org/codehaus/plexus/redback/users/messages", locale );
+
+            loadResource( properties, "org/codehaus/redback/i18n/default", null );
+            loadResource( properties, "org/codehaus/redback/i18n/default", locale );
+            return properties;
+        }
+        catch ( IOException e )
+        {
+            throw new RedbackServiceException( e.getMessage() );
+        }
+    }
+
+    private void loadResource( final Properties finalProperties, String resourceName, String locale )
+        throws IOException
+    {
+        InputStream is = null;
+        Properties properties = new Properties();
+        try
+        {
+            if ( StringUtils.isNotEmpty( locale ) )
+            {
+                resourceName = resourceName + "_" + locale;
+            }
+            resourceName = resourceName + ".properties";
+            is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
+            if ( is != null )
+            {
+                properties.load( is );
+                finalProperties.putAll( properties );
+            }
+            else
+            {
+                if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
+                {
+                    log.info( "cannot load resource {}", resourceName );
+                }
+            }
+        }
+        finally
+        {
+            IOUtils.closeQuietly( is );
+        }
+
+    }
+
+
+}

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUtilServices.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/DefaultUtilServices.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackAuthenticationThreadLocal.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackAuthenticationThreadLocal.java?rev=1310268&view=auto
==============================================================================
--- archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackAuthenticationThreadLocal.java (added)
+++ archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackAuthenticationThreadLocal.java Fri Apr  6 09:58:14 2012
@@ -0,0 +1,40 @@
+package org.codehaus.redback.rest.services;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+public class RedbackAuthenticationThreadLocal
+{
+    private static final ThreadLocal<RedbackRequestInformation> userThreadLocal =
+        new ThreadLocal<RedbackRequestInformation>();
+
+    public static void set( RedbackRequestInformation redbackRequestInformation )
+    {
+        userThreadLocal.set( redbackRequestInformation );
+    }
+
+    public static RedbackRequestInformation get()
+    {
+        return userThreadLocal.get();
+    }
+}

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackAuthenticationThreadLocal.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackAuthenticationThreadLocal.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackRequestInformation.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackRequestInformation.java?rev=1310268&view=auto
==============================================================================
--- archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackRequestInformation.java (added)
+++ archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackRequestInformation.java Fri Apr  6 09:58:14 2012
@@ -0,0 +1,58 @@
+package org.codehaus.redback.rest.services;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.codehaus.plexus.redback.users.User;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+public class RedbackRequestInformation
+{
+    private User user;
+
+    private String remoteAddr;
+
+    public RedbackRequestInformation( User user, String remoteAddr )
+    {
+        this.user = user;
+        this.remoteAddr = remoteAddr;
+    }
+
+    public User getUser()
+    {
+        return user;
+    }
+
+    public void setUser( User user )
+    {
+        this.user = user;
+    }
+
+    public String getRemoteAddr()
+    {
+        return remoteAddr;
+    }
+
+    public void setRemoteAddr( String remoteAddr )
+    {
+        this.remoteAddr = remoteAddr;
+    }
+}

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackRequestInformation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/RedbackRequestInformation.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/interceptors/AbstractInterceptor.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/interceptors/AbstractInterceptor.java?rev=1310268&view=auto
==============================================================================
--- archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/interceptors/AbstractInterceptor.java (added)
+++ archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/interceptors/AbstractInterceptor.java Fri Apr  6 09:58:14 2012
@@ -0,0 +1,69 @@
+package org.codehaus.redback.rest.services.interceptors;
+
+/*
+* Copyright 2011 The Codehaus.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*      http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+import org.apache.cxf.jaxrs.model.OperationResourceInfo;
+import org.apache.cxf.message.Message;
+import org.codehaus.plexus.redback.authorization.RedbackAuthorization;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Context;
+import java.lang.reflect.Method;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.3
+ */
+public class AbstractInterceptor
+{
+
+    private Logger log = LoggerFactory.getLogger( getClass() );
+
+    @Context
+    private HttpServletRequest httpServletRequest;
+
+    @Context
+    private HttpServletResponse httpServletResponse;
+
+    public HttpServletRequest getHttpServletRequest( Message message )
+    {
+        return httpServletRequest;
+    }
+
+    public HttpServletResponse getHttpServletResponse( Message message )
+    {
+        return httpServletResponse;
+    }
+
+    public RedbackAuthorization getRedbackAuthorization( Message message )
+    {
+        OperationResourceInfo operationResourceInfo = message.getExchange().get( OperationResourceInfo.class );
+        if ( operationResourceInfo == null )
+        {
+            return null;
+        }
+
+        Method method = operationResourceInfo.getAnnotatedMethod();
+
+        log.debug( " method name {}", method == null ? "null" : method.getName() );
+        RedbackAuthorization redbackAuthorization = method.getAnnotation( RedbackAuthorization.class );
+        return redbackAuthorization;
+    }
+}

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/interceptors/AbstractInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/redback/redback-core/trunk/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/codehaus/redback/rest/services/interceptors/AbstractInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision