1 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 50 public class CmsPasswordInfo { 51 52 53 private final CmsObject m_cms; 54 55 private String m_confirmation; 56 57 private String m_currentPwd; 58 59 private String m_newPwd; 60 61 private final String m_userName; 62 63 66 public CmsPasswordInfo() { 67 68 this(null); 69 } 70 71 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 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 104 public String getConfirmation() { 105 106 return m_confirmation; 107 } 108 109 114 public String getCurrentPwd() { 115 116 return m_currentPwd; 117 } 118 119 124 public String getNewPwd() { 125 126 return m_newPwd; 127 } 128 129 134 public void setConfirmation(String confirmation) { 135 136 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 151 public void setCurrentPwd(String 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 169 public void setNewPwd(String newPwd) { 170 171 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 |