You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by sh...@apache.org on 2016/06/24 07:05:24 UTC

[46/50] kylin git commit: refactor & cleanup UserService

refactor & cleanup UserService


Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/c7439a0a
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/c7439a0a
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/c7439a0a

Branch: refs/heads/stream_m1
Commit: c7439a0a4281d1633437f268fdb5d44fbcd6cde1
Parents: 4fd74fc
Author: Yang Li <li...@apache.org>
Authored: Fri Jun 24 08:10:39 2016 +0800
Committer: Yang Li <li...@apache.org>
Committed: Fri Jun 24 08:10:39 2016 +0800

----------------------------------------------------------------------
 .../kylin/rest/controller/UserController.java   |  2 +-
 .../apache/kylin/rest/security/UserManager.java | 29 -----------------
 .../apache/kylin/rest/service/UserService.java  | 33 +++++++++++---------
 .../kylin/rest/service/UserServiceTest.java     |  2 +-
 4 files changed, 20 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/c7439a0a/server/src/main/java/org/apache/kylin/rest/controller/UserController.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/controller/UserController.java b/server/src/main/java/org/apache/kylin/rest/controller/UserController.java
index a7bc782..54af010 100644
--- a/server/src/main/java/org/apache/kylin/rest/controller/UserController.java
+++ b/server/src/main/java/org/apache/kylin/rest/controller/UserController.java
@@ -76,7 +76,7 @@ public class UserController {
 
     @RequestMapping(value = "/authentication/authorities", method = RequestMethod.GET, produces = "application/json")
     public List<String> getAuthorities() {
-        return userService.getUserAuthorities();
+        return userService.listUserAuthorities();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/c7439a0a/server/src/main/java/org/apache/kylin/rest/security/UserManager.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/security/UserManager.java b/server/src/main/java/org/apache/kylin/rest/security/UserManager.java
deleted file mode 100644
index bbdbc2b..0000000
--- a/server/src/main/java/org/apache/kylin/rest/security/UserManager.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * 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.
-*/
-
-package org.apache.kylin.rest.security;
-
-import java.util.List;
-
-import org.springframework.security.provisioning.UserDetailsManager;
-
-public interface UserManager extends UserDetailsManager {
-
-    public List<String> getUserAuthorities();
-
-}

http://git-wip-us.apache.org/repos/asf/kylin/blob/c7439a0a/server/src/main/java/org/apache/kylin/rest/service/UserService.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/kylin/rest/service/UserService.java b/server/src/main/java/org/apache/kylin/rest/service/UserService.java
index a3b4293..d38a726 100644
--- a/server/src/main/java/org/apache/kylin/rest/service/UserService.java
+++ b/server/src/main/java/org/apache/kylin/rest/service/UserService.java
@@ -38,13 +38,13 @@ import org.apache.hadoop.hbase.client.Scan;
 import org.apache.kylin.common.util.Bytes;
 import org.apache.kylin.common.util.Pair;
 import org.apache.kylin.rest.security.AclHBaseStorage;
-import org.apache.kylin.rest.security.UserManager;
 import org.apache.kylin.rest.util.Serializer;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.userdetails.User;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.provisioning.UserDetailsManager;
 import org.springframework.stereotype.Component;
 
 import com.fasterxml.jackson.core.JsonParseException;
@@ -52,11 +52,9 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonMappingException;
 
 /**
- * @author xduo
- * 
  */
 @Component("userService")
-public class UserService implements UserManager {
+public class UserService implements UserDetailsManager {
 
     private static final String PWD_PREFIX = "PWD:";
 
@@ -201,12 +199,23 @@ public class UserService implements UserManager {
         }
     }
 
-    @Override
-    public List<String> getUserAuthorities() {
+    public List<String> listUserAuthorities() {
+        List<String> all = new ArrayList<String>();
+        for (UserDetails user : listUsers()) {
+            for (GrantedAuthority auth : user.getAuthorities()) {
+                if (!all.contains(auth.getAuthority())) {
+                    all.add(auth.getAuthority());
+                }
+            }
+        }
+        return all;
+    }
+    
+    public List<UserDetails> listUsers() {
         Scan s = new Scan();
         s.addColumn(Bytes.toBytes(AclHBaseStorage.USER_AUTHORITY_FAMILY), Bytes.toBytes(AclHBaseStorage.USER_AUTHORITY_COLUMN));
 
-        List<String> all = new ArrayList<String>();
+        List<UserDetails> all = new ArrayList<UserDetails>();
         HTableInterface htable = null;
         ResultScanner scanner = null;
         try {
@@ -215,20 +224,14 @@ public class UserService implements UserManager {
 
             for (Result result = scanner.next(); result != null; result = scanner.next()) {
                 User user = hbaseRowToUser(result);
-
-                for (GrantedAuthority auth : user.getAuthorities()) {
-                    if (!all.contains(auth.getAuthority())) {
-                        all.add(auth.getAuthority());
-                    }
-                }
+                all.add(user);
             }
         } catch (IOException e) {
-            throw new RuntimeException(e.getMessage(), e);
+            throw new RuntimeException("Failed to scan users", e);
         } finally {
             IOUtils.closeQuietly(scanner);
             IOUtils.closeQuietly(htable);
         }
-
         return all;
     }
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/c7439a0a/server/src/test/java/org/apache/kylin/rest/service/UserServiceTest.java
----------------------------------------------------------------------
diff --git a/server/src/test/java/org/apache/kylin/rest/service/UserServiceTest.java b/server/src/test/java/org/apache/kylin/rest/service/UserServiceTest.java
index 1bf3ce8..28515be 100644
--- a/server/src/test/java/org/apache/kylin/rest/service/UserServiceTest.java
+++ b/server/src/test/java/org/apache/kylin/rest/service/UserServiceTest.java
@@ -55,6 +55,6 @@ public class UserServiceTest extends ServiceTestBase {
         Assert.assertEquals("ROLE_ADMIN", ud.getAuthorities().iterator().next().getAuthority());
         Assert.assertEquals(1, ud.getAuthorities().size());
 
-        Assert.assertTrue(userService.getUserAuthorities().contains("ROLE_ADMIN"));
+        Assert.assertTrue(userService.listUserAuthorities().contains("ROLE_ADMIN"));
     }
 }