6c6f07ca663bdebfefad7a64c320be17777dd9fe
[controller.git] / opendaylight / usermanager / src / main / java / org / opendaylight / controller / usermanager / internal / AuthenticatedUser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.usermanager.internal;
10
11 import java.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.Date;
14 import java.util.List;
15
16 import org.opendaylight.controller.sal.authorization.UserLevel;
17 import org.opendaylight.controller.usermanager.ODLUserLevel;
18 import org.springframework.security.core.GrantedAuthority;
19 import org.springframework.security.core.authority.SimpleGrantedAuthority;
20
21 /**
22  * Represents a user that was successfully authenticated and authorized
23  * It contains the user role for which the user was authorized and the
24  * date on which it was authenticated and authorized
25  */
26 public class AuthenticatedUser implements Serializable {
27     private static final long serialVersionUID = 1L;
28     private List<String> userRoles;
29     private Date accessDate;
30
31     public AuthenticatedUser(String name) {
32         userRoles = null;
33         accessDate = new Date();
34     }
35
36     public void setRoleList(List<String> roleList) {
37         this.userRoles = roleList;
38     }
39
40     public void setRoleList(String[] roleArray) {
41         userRoles = new ArrayList<String>(roleArray.length);
42         for (String role : roleArray) {
43             userRoles.add(role);
44         }
45     }
46
47     public List<String> getUserRoles() {
48         return userRoles;
49     }
50
51     public void addUserRole(String string) {
52         userRoles.add(string);
53     }
54
55     public String getAccessDate() {
56         return accessDate.toString();
57     }
58
59     public List<GrantedAuthority> getGrantedAuthorities(UserLevel usrLvl) {
60         List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
61         roles.add(new SimpleGrantedAuthority(new ODLUserLevel(usrLvl)
62                 .getAuthority()));
63         return roles;
64     }
65
66 }