OpenDaylight Controller functional modules.
[controller.git] / opendaylight / usermanager / src / main / java / org / opendaylight / controller / usermanager / internal / UserConfig.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
13 import org.apache.commons.lang3.builder.EqualsBuilder;
14 import org.apache.commons.lang3.builder.HashCodeBuilder;
15 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
16 import org.opendaylight.controller.usermanager.AuthResponse;
17
18 /**
19  * Configuration Java Object which represents a Local AAA user
20  * configuration information for User Manager. 
21  */
22 public class UserConfig implements Serializable {
23         private static final long serialVersionUID = 1L;
24
25         /*
26          * Clear text password as we are moving to some MD5 digest
27          * for when saving configurations
28          */
29         protected String user;
30         protected String role;
31         private String password;
32
33         public UserConfig() {
34         }
35
36         public UserConfig(String user, String password, String role) {
37                 this.user = user;
38                 this.password = password;
39                 this.role = role;
40         }
41
42         public String getUser() {
43                 return user;
44         }
45
46         public String getPassword() {
47                 return password;
48         }
49
50         public String getRole() {
51                 return role;
52         }
53
54     @Override
55     public int hashCode() {
56         return HashCodeBuilder.reflectionHashCode(this);
57     }
58
59     @Override
60     public boolean equals(Object obj) {
61         return EqualsBuilder.reflectionEquals(this, obj);
62     }
63     
64     @Override
65     public String toString() {
66         return "UserConfig[user="+ user + ", password=" + password + "]";
67     }
68
69         public boolean isValid() {
70                 return (user != null && !user.isEmpty() && role != null
71                                 && !role.isEmpty() && password != null && !password.isEmpty());
72         }
73
74         public boolean update(String currentPassword, String newPassword,
75                         String newRole) {
76                 // To make any changes to a user configured profile, current password
77                 // must always be provided
78                 if (!this.password.equals(currentPassword)) {
79                         return false;
80                 }
81                 if (newPassword != null) {
82                         this.password = newPassword;
83                 }
84                 if (newRole != null) {
85                         this.role = newRole;
86                 }
87                 return true;
88         }
89
90         public AuthResponse authenticate(String clearTextPass) {
91                 AuthResponse locResponse = new AuthResponse();
92                 if (password.equals(clearTextPass)) {
93                         locResponse.setStatus(AuthResultEnum.AUTH_ACCEPT_LOC);
94                         locResponse.addData(role.replace(",", " "));
95                 } else {
96                         locResponse.setStatus(AuthResultEnum.AUTH_REJECT_LOC);
97                 }
98                 return locResponse;
99         }
100 }