KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > security > CmsPasswordInfo


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/security/CmsPasswordInfo.java,v $
3  * Date : $Date: 2005/06/23 11:11:44 $
4  * Version: $Revision: 1.7 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.security;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.main.CmsIllegalStateException;
38 import org.opencms.main.OpenCms;
39 import org.opencms.util.CmsStringUtil;
40
41 /**
42  * Validating bean for changing the password.<p>
43  *
44  * @author Michael Moossen
45  *
46  * @version $Revision: 1.7 $
47  *
48  * @since 6.0.0
49  */

50 public class CmsPasswordInfo {
51
52     /** Cms Context. */
53     private final CmsObject m_cms;
54     /** Password Confirmation. */
55     private String JavaDoc m_confirmation;
56     /** Current (old) users password. */
57     private String JavaDoc m_currentPwd;
58     /** New Password. */
59     private String JavaDoc m_newPwd;
60     /** Current logged in user name. */
61     private final String JavaDoc m_userName;
62
63     /**
64      * Default Constructor.<p>
65      */

66     public CmsPasswordInfo() {
67
68         this(null);
69     }
70
71     /**
72      * Use this Constructor if you need to check the old password of the current logged in user.<p>
73      *
74      * @param cms the cms context
75      */

76     public CmsPasswordInfo(CmsObject cms) {
77
78         m_cms = cms;
79         if (m_cms != null) {
80             m_userName = m_cms.getRequestContext().currentUser().getName();
81         } else {
82             m_userName = null;
83         }
84     }
85
86     /**
87      * Sets the new password for the current logged in user.<p>
88      *
89      * @throws CmsException if something goes wrong
90      */

91     public void applyChanges() throws CmsException {
92
93         if (m_userName == null) {
94             throw new CmsIllegalStateException(Messages.get().container(Messages.ERR_INVALID_USER_CONTEXT_0));
95         }
96         m_cms.setPassword(m_userName, getCurrentPwd(), getNewPwd());
97     }
98
99     /**
100      * Returns the confirmation.<p>
101      *
102      * @return the confirmation
103      */

104     public String JavaDoc getConfirmation() {
105
106         return m_confirmation;
107     }
108
109     /**
110      * Returns the current Pwd.<p>
111      *
112      * @return the current Pwd
113      */

114     public String JavaDoc getCurrentPwd() {
115
116         return m_currentPwd;
117     }
118
119     /**
120      * Returns the new Pwd.<p>
121      *
122      * @return the new Pwd
123      */

124     public String JavaDoc getNewPwd() {
125
126         return m_newPwd;
127     }
128
129     /**
130      * Sets the confirmation.<p>
131      *
132      * @param confirmation the confirmation to set
133      */

134     public void setConfirmation(String JavaDoc confirmation) {
135
136         // leave password unchanged, if the new password and the confirmation is empty
137
if (CmsStringUtil.isEmpty(getNewPwd()) && CmsStringUtil.isEmpty(confirmation)) {
138             return;
139         }
140         if (!getNewPwd().equals(confirmation)) {
141             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_NEWPWD_MISMATCH_0));
142         }
143         m_confirmation = confirmation;
144     }
145
146     /**
147      * Sets the current Pwd.<p>
148      *
149      * @param currentPwd the current Pwd to set
150      */

151     public void setCurrentPwd(String JavaDoc currentPwd) {
152
153         if (m_userName == null) {
154             throw new CmsIllegalStateException(Messages.get().container(Messages.ERR_INVALID_USER_CONTEXT_0));
155         }
156         try {
157             m_cms.readUser(m_userName, currentPwd);
158         } catch (CmsException e) {
159             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_USER_PWD_1, m_userName));
160         }
161         m_currentPwd = currentPwd;
162     }
163
164     /**
165      * Sets the new Pwd.<p>
166      *
167      * @param newPwd the new Pwd to set
168      */

169     public void setNewPwd(String JavaDoc newPwd) {
170
171         // leave password unchanged, if the new password is empty
172
if (CmsStringUtil.isEmpty(newPwd)) {
173             return;
174         }
175         try {
176             OpenCms.getPasswordHandler().validatePassword(newPwd);
177         } catch (CmsSecurityException e) {
178             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_NEWPWD_0), e);
179         }
180         m_newPwd = newPwd;
181     }
182 }
Popular Tags