/**
 * Copyright (c) 2015 Bosch Software Innovations GmbH and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipse.hawkbit.security;

import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Auditor class that allows {@link BaseEntity}s to insert currenlt logged in
 * user for repository changes.
 *
 *
 *
 *
 *
 */
public class SpringSecurityAuditorAware implements AuditorAware<String> {

    @Override
    public String getCurrentAuditor() {

        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }

        if (authentication.getPrincipal() != null) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                return ((UserDetails) authentication.getPrincipal()).getUsername();
            }
            return authentication.getPrincipal().toString();
        }
        return null;
    }
}

@@ -641,15 +641,8 @@
 ows 
-%7B@link 
 Base
@@ -647,17 +647,16 @@
 seEntity
-%7D
 s to ins
@@ -665,17 +665,16 @@
 t curren
-l
 t logged
@@ -676,19 +676,16 @@
 ogged in
-%0A *
  user fo
@@ -685,16 +685,19 @@
 user for
+%0A *
  reposit
@@ -712,28 +712,16 @@
 ges.%0A *%0A
- *%0A *%0A *%0A *%0A
  */%0Apubl
@@ -965,35 +965,33 @@
 if (
-a
+isA
 uthenti
+fa
 c
-a
 tion
- == null %7C%7C !
+Invalid(
 auth
@@ -1004,25 +1004,8 @@
 tion
-.isAuthenticated(
 )) %7B
@@ -1053,57 +1053,133 @@
     
-if (authentication.getPrincipal() != null) %7B%0A    
+return getCurrentAuditor(authentication);%0A    %7D%0A%0A    private String getCurrentAuditor(final Authentication authentication) %7B%0A
     
@@ -1250,28 +1250,24 @@
             
- 
-   
 return ((Use
@@ -1326,34 +1326,26 @@
 ();%0A        
-    %7D%0A    
+%7D%0A
         retu
@@ -1393,36 +1393,220 @@
 ();%0A    
+%7D%0A%0A
     
-%7D%0A        return
+private static boolean isAuthentifactionInvalid(final Authentication authentication) %7B%0A        return authentication == null %7C%7C !authentication.isAuthenticated() %7C%7C authentication.getPrincipal() ==
  null;%0A 

Refactor auditore aware