[SYNCOPE-690] Implementation completed

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

Branch: refs/heads/SYNCOPE-156
Commit: e9839b3590e04a46e6513fab47e2793a130d3fbb
Parents: f1880e2
Author: Francesco Chicchiriccò <[email protected]>
Authored: Mon Sep 7 17:26:43 2015 +0200
Committer: Francesco Chicchiriccò <[email protected]>
Committed: Mon Sep 7 17:26:43 2015 +0200

----------------------------------------------------------------------
 .../console/SyncopeConsoleApplication.java      |   8 +-
 .../console/pages/MustChangePassword.java       | 120 +++++++++++++++++++
 .../client/console/rest/UserSelfRestClient.java |  10 +-
 .../html/form/AjaxPasswordFieldPanel.java       |  16 +++
 .../console/pages/MustChangePassword.html       |  62 ++++++++++
 .../console/pages/MustChangePassword.properties |  21 ++++
 .../pages/MustChangePassword_it.properties      |  21 ++++
 .../pages/MustChangePassword_pt_BR.properties   |  21 ++++
 .../html/form/AjaxPasswordFieldPanel.html       |   9 +-
 .../client/lib/RestClientExceptionMapper.java   |  11 +-
 .../syncope/common/lib/AnyOperations.java       |   2 +
 .../apache/syncope/common/lib/mod/UserMod.java  |  13 +-
 .../apache/syncope/common/lib/to/UserTO.java    |  10 ++
 .../common/lib/types/ClientExceptionType.java   |   2 +-
 .../syncope/common/lib/types/Entitlement.java   |   4 +
 .../rest/api/service/UserSelfService.java       |  14 ++-
 .../syncope/core/logic/AbstractAnyLogic.java    |   4 +-
 .../apache/syncope/core/logic/GroupLogic.java   |   4 +-
 .../apache/syncope/core/logic/UserLogic.java    |  14 ++-
 .../core/misc/security/AuthDataAccessor.java    |  76 ++++++------
 .../DelegatedAdministrationException.java       |  33 +++++
 .../misc/security/MustChangePasswordFilter.java |  80 +++++++++++++
 .../security/SyncopeAccessDeniedHandler.java    |  43 +++++++
 .../SyncopeAuthenticationEntryPoint.java        |  43 +++++++
 .../misc/security/UnauthorizedException.java    |  33 -----
 .../misc/src/main/resources/securityContext.xml |  19 ++-
 .../core/persistence/api/entity/user/User.java  |   4 +
 .../persistence/jpa/dao/JPAAnyObjectDAO.java    |   4 +-
 .../core/persistence/jpa/dao/JPAGroupDAO.java   |   4 +-
 .../core/persistence/jpa/dao/JPAUserDAO.java    |   4 +-
 .../persistence/jpa/entity/user/JPAUser.java    |  34 +++---
 .../java/data/UserDataBinderImpl.java           |   6 +-
 .../java/sync/AbstractSyncResultHandler.java    |   4 +-
 .../rest/cxf/RestServiceExceptionMapper.java    |  13 +-
 .../rest/cxf/service/UserSelfServiceImpl.java   |  12 +-
 .../core/reference/AuthenticationITCase.java    |   8 +-
 .../syncope/fit/core/reference/GroupITCase.java |   2 +-
 .../fit/core/reference/UserSelfITCase.java      |  32 +++++
 38 files changed, 685 insertions(+), 135 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleApplication.java
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleApplication.java
 
b/client/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleApplication.java
index 2bff477..dd12e64 100644
--- 
a/client/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleApplication.java
+++ 
b/client/console/src/main/java/org/apache/syncope/client/console/SyncopeConsoleApplication.java
@@ -24,14 +24,17 @@ import java.util.List;
 import java.util.Locale;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.client.console.pages.BasePage;
+import org.apache.syncope.client.console.pages.MustChangePassword;
 import org.apache.syncope.client.console.pages.Dashboard;
 import org.apache.syncope.client.console.pages.Login;
 import org.apache.syncope.client.console.resources.FilesystemResource;
 import org.apache.syncope.client.console.resources.WorkflowDefGETResource;
 import org.apache.syncope.client.console.resources.WorkflowDefPUTResource;
+import org.apache.syncope.common.lib.types.Entitlement;
 import org.apache.wicket.Page;
 import 
org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
 import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
+import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
 import 
org.apache.wicket.authroles.authorization.strategies.role.metadata.MetaDataRoleAuthorizationStrategy;
 import org.apache.wicket.markup.html.WebPage;
 import org.apache.wicket.protocol.http.WebApplication;
@@ -132,7 +135,10 @@ public class SyncopeConsoleApplication extends 
AuthenticatedWebApplication {
 
     @Override
     public Class<? extends Page> getHomePage() {
-        return Dashboard.class;
+        return AuthenticatedWebSession.get().isSignedIn()
+                && 
SyncopeConsoleSession.get().owns(Entitlement.MUST_CHANGE_PASSWORD)
+                        ? MustChangePassword.class
+                        : Dashboard.class;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/java/org/apache/syncope/client/console/pages/MustChangePassword.java
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/java/org/apache/syncope/client/console/pages/MustChangePassword.java
 
b/client/console/src/main/java/org/apache/syncope/client/console/pages/MustChangePassword.java
new file mode 100644
index 0000000..8c1e341
--- /dev/null
+++ 
b/client/console/src/main/java/org/apache/syncope/client/console/pages/MustChangePassword.java
@@ -0,0 +1,120 @@
+/*
+ * 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.syncope.client.console.pages;
+
+import org.apache.syncope.client.console.SyncopeConsoleSession;
+import org.apache.syncope.client.console.commons.Constants;
+import org.apache.syncope.client.console.panels.NotificationPanel;
+import org.apache.syncope.client.console.rest.UserSelfRestClient;
+import 
org.apache.syncope.client.console.wicket.markup.html.form.AjaxPasswordFieldPanel;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.markup.html.form.AjaxButton;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.PasswordTextField;
+import org.apache.wicket.markup.html.form.StatelessForm;
+import org.apache.wicket.markup.html.form.TextField;
+import 
org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.spring.injection.annot.SpringBean;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MustChangePassword extends WebPage {
+
+    private static final long serialVersionUID = 5889157642852559004L;
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(MustChangePassword.class);
+
+    @SpringBean
+    private UserSelfRestClient userSelfRestClient;
+
+    private final NotificationPanel feedbackPanel;
+
+    private final StatelessForm<Void> form;
+
+    private final TextField<String> usernameField;
+
+    private final AjaxPasswordFieldPanel passwordField;
+
+    private final AjaxPasswordFieldPanel confirmPasswordField;
+
+    public MustChangePassword(final PageParameters parameters) {
+        super(parameters);
+
+        feedbackPanel = new NotificationPanel(Constants.FEEDBACK);
+        add(feedbackPanel);
+
+        form = new StatelessForm<>("changePassword");
+        form.setOutputMarkupId(true);
+
+        usernameField = new TextField<>("username", new 
Model<>(SyncopeConsoleSession.get().getSelfTO().getUsername()));
+        usernameField.setMarkupId("username");
+        usernameField.setEnabled(false);
+        form.add(usernameField);
+
+        passwordField = new AjaxPasswordFieldPanel("password", "password", new 
Model<String>());
+        passwordField.setRequired(true);
+        passwordField.setMarkupId("password");
+        passwordField.setPlaceholder(getString("password"));
+        ((PasswordTextField) passwordField.getField()).setResetPassword(true);
+        form.add(passwordField);
+
+        confirmPasswordField = new AjaxPasswordFieldPanel("confirmPassword", 
"confirmPassword", new Model<String>());
+        confirmPasswordField.setRequired(true);
+        confirmPasswordField.setMarkupId("confirmPassword");
+        confirmPasswordField.setPlaceholder(getString("confirmPassword"));
+        ((PasswordTextField) 
confirmPasswordField.getField()).setResetPassword(true);
+        form.add(confirmPasswordField);
+
+        form.add(new EqualPasswordInputValidator(passwordField.getField(), 
confirmPasswordField.getField()));
+
+        AjaxButton submitButton = new AjaxButton("submit", new 
Model<>(getString("submit"))) {
+
+            private static final long serialVersionUID = 429178684321093953L;
+
+            @Override
+            protected void onSubmit(final AjaxRequestTarget target, final 
Form<?> form) {
+                try {
+                    
userSelfRestClient.changePassword(passwordField.getModelObject());
+
+                    SyncopeConsoleSession.get().invalidate();
+
+                    setResponsePage(getApplication().getHomePage());
+                } catch (Exception e) {
+                    LOG.error("While changing password for {}",
+                            
SyncopeConsoleSession.get().getSelfTO().getUsername(), e);
+                    error(getString(Constants.ERROR) + ": " + e.getMessage());
+                    feedbackPanel.refresh(target);
+                }
+            }
+
+            @Override
+            protected void onError(final AjaxRequestTarget target, final 
Form<?> form) {
+                feedbackPanel.refresh(target);
+            }
+
+        };
+        form.add(submitButton);
+        form.setDefaultButton(submitButton);
+
+        add(form);
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/java/org/apache/syncope/client/console/rest/UserSelfRestClient.java
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/java/org/apache/syncope/client/console/rest/UserSelfRestClient.java
 
b/client/console/src/main/java/org/apache/syncope/client/console/rest/UserSelfRestClient.java
index 9668672..dc71a54 100644
--- 
a/client/console/src/main/java/org/apache/syncope/client/console/rest/UserSelfRestClient.java
+++ 
b/client/console/src/main/java/org/apache/syncope/client/console/rest/UserSelfRestClient.java
@@ -40,7 +40,7 @@ public class UserSelfRestClient extends BaseRestClient {
 
         return result == null
                 ? false
-                : result.booleanValue();
+                : result;
     }
 
     public UserTO read() {
@@ -69,7 +69,7 @@ public class UserSelfRestClient extends BaseRestClient {
 
         return result == null
                 ? false
-                : result.booleanValue();
+                : result;
     }
 
     public boolean isPwdResetRequiringSecurityQuestions() {
@@ -82,7 +82,11 @@ public class UserSelfRestClient extends BaseRestClient {
 
         return result == null
                 ? false
-                : result.booleanValue();
+                : result;
+    }
+
+    public void changePassword(final String password) {
+        getService(UserSelfService.class).changePassword(password);
     }
 
     public void requestPasswordReset(final String username, final String 
securityAnswer) {

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.java
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.java
 
b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.java
index 2eb11ea..e40a630 100644
--- 
a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.java
+++ 
b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.java
@@ -19,8 +19,11 @@
 package org.apache.syncope.client.console.wicket.markup.html.form;
 
 import org.apache.syncope.client.console.commons.Constants;
+import org.apache.wicket.Component;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
+import org.apache.wicket.behavior.Behavior;
+import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.html.form.PasswordTextField;
 import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.Model;
@@ -57,4 +60,17 @@ public class AjaxPasswordFieldPanel extends 
FieldPanel<String> {
         this.isRequiredLabelAdded = true;
         return this;
     }
+
+    public void setPlaceholder(final String placeholder) {
+        field.add(new Behavior() {
+
+            private static final long serialVersionUID = 1469628524240283489L;
+
+            @Override
+            public void onComponentTag(final Component component, final 
ComponentTag tag) {
+                tag.put("placeholder", placeholder);
+            }
+        });
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.html
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.html
 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.html
new file mode 100644
index 0000000..675c4df
--- /dev/null
+++ 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<!--
+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.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:wicket="http://wicket.apache.org";>
+  <head>
+    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
+    <meta charset="UTF-8"/>
+    <meta content='width=device-width, initial-scale=1, maximum-scale=1, 
user-scalable=no' name='viewport'/>
+
+    <title>Apache Syncope - Change password</title>
+
+    <link rel="shortcut icon" href="img/favicon.png" type="image/png"/>
+
+    <link href="webjars/bootstrap/${bootstrap.version}/css/bootstrap.min.css" 
rel="stylesheet" type="text/css" />
+    <link 
href="webjars/bootstrap-select/${bootstrap-select.version}/css/bootstrap-select.min.css"
 rel="stylesheet" type="text/css" />
+    <link 
href="webjars/font-awesome/${font-awesome.version}/css/font-awesome.min.css" 
rel="stylesheet" type="text/css" />
+    <link href="webjars/ionicons/${ionicons.version}/css/ionicons.min.css" 
rel="stylesheet" type="text/css" />
+    <link href="css/AdminLTE.css" rel="stylesheet" type="text/css" />
+    <link href="css/login.css" rel="stylesheet" type="text/css" />
+    <link href="css/syncopeConsole.css" rel="stylesheet" type="text/css" />
+
+    <script type="text/javascript" 
src="webjars/bootstrap/${bootstrap.version}/js/bootstrap.min.js"></script>
+    <script type="text/javascript" 
src="webjars/bootstrap-select/${bootstrap-select.version}/js/bootstrap-select.min.js"></script>
+  </head>
+  <body class="skin-green">
+    <div class="container">
+      <div class="card card-container">
+        <img class="login-logo" src="img/logo-green.png" />
+
+        <div wicket:id="feedback" role="alert"/>
+
+        <div class="alert alert-success"><wicket:message 
key="passwordNeedsToBeUpdated"/></div>
+
+        <form class="form-signin" wicket:id="changePassword">
+          <input type="text" wicket:id="username" id="username" 
class="form-control" 
+                 wicket:message="placeholder:username" required="required" 
autofocus="autofocus" />
+          <span wicket:id="password" id="password"/>
+          <span wicket:id="confirmPassword" id="confirmPassword"/>
+          <button wicket:id="submit" type="submit" 
+                  class="btn btn-lg btn-primary btn-block 
btn-signin"><wicket:message key="submit"/></button>
+        </form>
+      </div>
+    </div>
+
+  </body>
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.properties
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.properties
 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.properties
new file mode 100644
index 0000000..26662c0
--- /dev/null
+++ 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword.properties
@@ -0,0 +1,21 @@
+# 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.
+username=Username
+password=Password
+submit=Change
+confirmPassword=Password (confirm)
+passwordNeedsToBeUpdated=Password needs to be updated

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_it.properties
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_it.properties
 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_it.properties
new file mode 100644
index 0000000..45abc3d
--- /dev/null
+++ 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_it.properties
@@ -0,0 +1,21 @@
+# 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.
+username=Username
+password=Password
+submit=Cambia
+confirmPassword=Password (conferma)
+passwordNeedsToBeUpdated=La password deve essere aggiornata

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_pt_BR.properties
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_pt_BR.properties
 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_pt_BR.properties
new file mode 100644
index 0000000..87d5ad0
--- /dev/null
+++ 
b/client/console/src/main/resources/org/apache/syncope/client/console/pages/MustChangePassword_pt_BR.properties
@@ -0,0 +1,21 @@
+# 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.
+username=Usu\u00e1rio
+password=Senha
+submit=Alterar
+confirmPassword=Senha (confirmar)
+passwordNeedsToBeUpdated=Senha precisa ser atualizado

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.html
----------------------------------------------------------------------
diff --git 
a/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.html
 
b/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.html
index 1f7b8f3..eab74ac 100644
--- 
a/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.html
+++ 
b/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/AjaxPasswordFieldPanel.html
@@ -16,7 +16,8 @@ KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 -->
-<wicket:extend>
-    <input type="password" class="ui-widget-content ui-corner-all 
long_dynamicsize"
-           wicket:id="passwordField" />
-</wicket:extend>
+<html xmlns="http://www.w3.org/1999/xhtml"; 
xmlns:wicket="http://wicket.apache.org";>
+  <wicket:extend>
+    <input type="password" class="form-control" wicket:id="passwordField" />
+  </wicket:extend>
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/client/lib/src/main/java/org/apache/syncope/client/lib/RestClientExceptionMapper.java
----------------------------------------------------------------------
diff --git 
a/client/lib/src/main/java/org/apache/syncope/client/lib/RestClientExceptionMapper.java
 
b/client/lib/src/main/java/org/apache/syncope/client/lib/RestClientExceptionMapper.java
index 6649524..7a88245 100644
--- 
a/client/lib/src/main/java/org/apache/syncope/client/lib/RestClientExceptionMapper.java
+++ 
b/client/lib/src/main/java/org/apache/syncope/client/lib/RestClientExceptionMapper.java
@@ -61,9 +61,14 @@ public class RestClientExceptionMapper implements 
ExceptionMapper<Exception>, Re
             ex = scce.getExceptions().size() == 1
                     ? scce.getExceptions().iterator().next()
                     : scce;
-        } else if (statusCode == Response.Status.UNAUTHORIZED.getStatusCode()) 
{
-            // 2. Map SC_UNAUTHORIZED
-            ex = new AccessControlException("Remote unauthorized exception");
+        } else if (statusCode == Response.Status.UNAUTHORIZED.getStatusCode()
+                || statusCode == Response.Status.FORBIDDEN.getStatusCode()) {
+
+            // 2. Map SC_UNAUTHORIZED and SC_FORBIDDEN
+            String message = response.getHeaderString(RESTHeaders.ERROR_INFO);
+            ex = new AccessControlException(StringUtils.isBlank(message)
+                    ? "Remote authorization exception"
+                    : message);
         } else if (statusCode == Response.Status.BAD_REQUEST.getStatusCode()) {
             // 3. Map SC_BAD_REQUEST
             ex = new BadRequestException();

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/common/lib/src/main/java/org/apache/syncope/common/lib/AnyOperations.java
----------------------------------------------------------------------
diff --git 
a/common/lib/src/main/java/org/apache/syncope/common/lib/AnyOperations.java 
b/common/lib/src/main/java/org/apache/syncope/common/lib/AnyOperations.java
index e8bcedb..b29ff9e 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/AnyOperations.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/AnyOperations.java
@@ -305,6 +305,8 @@ public final class AnyOperations {
             result.setSecurityAnswer(updated.getSecurityAnswer());
         }
 
+        result.setMustChangePassword(updated.isMustChangePassword());
+
         // 4. roles
         
result.getRolesToRemove().addAll(CollectionUtils.subtract(original.getRoles(), 
updated.getRoles()));
         
result.getRolesToAdd().addAll(CollectionUtils.subtract(updated.getRoles(), 
original.getRoles()));

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/common/lib/src/main/java/org/apache/syncope/common/lib/mod/UserMod.java
----------------------------------------------------------------------
diff --git 
a/common/lib/src/main/java/org/apache/syncope/common/lib/mod/UserMod.java 
b/common/lib/src/main/java/org/apache/syncope/common/lib/mod/UserMod.java
index fe09eeb..95059fe 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/mod/UserMod.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/mod/UserMod.java
@@ -57,6 +57,8 @@ public class UserMod extends AnyMod {
 
     private String securityAnswer;
 
+    private boolean mustChangePassword;
+
     public String getUsername() {
         return username;
     }
@@ -139,6 +141,14 @@ public class UserMod extends AnyMod {
         this.securityAnswer = securityAnswer;
     }
 
+    public boolean isMustChangePassword() {
+        return mustChangePassword;
+    }
+
+    public void setMustChangePassword(final boolean mustChangePassword) {
+        this.mustChangePassword = mustChangePassword;
+    }
+
     @JsonIgnore
     @Override
     public boolean isEmpty() {
@@ -147,6 +157,7 @@ public class UserMod extends AnyMod {
                 && username == null
                 && pwdPropRequest == null
                 && securityQuestion == null
-                && securityAnswer == null;
+                && securityAnswer == null
+                && !mustChangePassword;
     }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/common/lib/src/main/java/org/apache/syncope/common/lib/to/UserTO.java
----------------------------------------------------------------------
diff --git 
a/common/lib/src/main/java/org/apache/syncope/common/lib/to/UserTO.java 
b/common/lib/src/main/java/org/apache/syncope/common/lib/to/UserTO.java
index dd5ebee..7018cd5 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/to/UserTO.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/to/UserTO.java
@@ -63,6 +63,8 @@ public class UserTO extends AnyTO {
 
     private String securityAnswer;
 
+    private boolean mustChangePassword;
+
     private final List<RelationshipTO> relationships = new ArrayList<>();
 
     private final List<MembershipTO> memberships = new ArrayList<>();
@@ -169,6 +171,14 @@ public class UserTO extends AnyTO {
         this.securityAnswer = securityAnswer;
     }
 
+    public boolean isMustChangePassword() {
+        return mustChangePassword;
+    }
+
+    public void setMustChangePassword(final boolean mustChangePassword) {
+        this.mustChangePassword = mustChangePassword;
+    }
+
     @XmlElementWrapper(name = "relationships")
     @XmlElement(name = "relationship")
     @JsonProperty("relationships")

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
----------------------------------------------------------------------
diff --git 
a/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
 
b/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
index d85d1b6..0c19403 100644
--- 
a/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
+++ 
b/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
@@ -66,7 +66,7 @@ public enum ClientExceptionType {
     RESTValidation(Response.Status.BAD_REQUEST),
     GroupOwnership(Response.Status.BAD_REQUEST),
     Scheduling(Response.Status.BAD_REQUEST),
-    Unauthorized(Response.Status.UNAUTHORIZED),
+    DelegatedAdministration(Response.Status.FORBIDDEN),
     UnallowedSchemas(Response.Status.BAD_REQUEST),
     Unknown(Response.Status.BAD_REQUEST),
     Workflow(Response.Status.BAD_REQUEST);

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/common/lib/src/main/java/org/apache/syncope/common/lib/types/Entitlement.java
----------------------------------------------------------------------
diff --git 
a/common/lib/src/main/java/org/apache/syncope/common/lib/types/Entitlement.java 
b/common/lib/src/main/java/org/apache/syncope/common/lib/types/Entitlement.java
index ccb4fce..5a4ea81 100644
--- 
a/common/lib/src/main/java/org/apache/syncope/common/lib/types/Entitlement.java
+++ 
b/common/lib/src/main/java/org/apache/syncope/common/lib/types/Entitlement.java
@@ -28,6 +28,8 @@ public final class Entitlement {
 
     public static final String ANONYMOUS = "ANONYMOUS";
 
+    public static final String MUST_CHANGE_PASSWORD = "MUST_CHANGE_PASSWORD";
+
     public static final String DOMAIN_CREATE = "DOMAIN_CREATE";
 
     public static final String DOMAIN_READ = "DOMAIN_READ";
@@ -251,6 +253,8 @@ public final class Entitlement {
                 values.add(field.getName());
             }
         }
+        values.remove(ANONYMOUS);
+        values.remove(MUST_CHANGE_PASSWORD);
         ENTITLEMENTS = Collections.unmodifiableSet(values);
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/UserSelfService.java
----------------------------------------------------------------------
diff --git 
a/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/UserSelfService.java
 
b/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/UserSelfService.java
index 2203a11..258a1b6 100644
--- 
a/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/UserSelfService.java
+++ 
b/common/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/UserSelfService.java
@@ -65,7 +65,7 @@ public interface UserSelfService extends JAXRSService {
     /**
      * Self-updates user.
      *
-     * @param userMod modification to be applied to user matching the provided 
key
+     * @param userMod modification to be applied to self
      * @return <tt>Response</tt> object featuring the updated user - 
<tt>UserTO</tt> as <tt>Entity</tt>
      */
     @POST
@@ -84,6 +84,18 @@ public interface UserSelfService extends JAXRSService {
     Response delete();
 
     /**
+     * Changes own password.
+     *
+     * @param password the password value to update
+     *
+     * @return <tt>Response</tt> object featuring the updated user - 
<tt>UserTO</tt> as <tt>Entity</tt>
+     */
+    @POST
+    @Path("changePassword")
+    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
+    Response changePassword(@NotNull @QueryParam("password") String password);
+
+    /**
      * Provides answer for the security question configured for user matching 
the given username, if any.
      * If provided anwser matches the one stored for that user, a password 
reset token is internally generated,
      * otherwise an error is returned.

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/logic/src/main/java/org/apache/syncope/core/logic/AbstractAnyLogic.java
----------------------------------------------------------------------
diff --git 
a/core/logic/src/main/java/org/apache/syncope/core/logic/AbstractAnyLogic.java 
b/core/logic/src/main/java/org/apache/syncope/core/logic/AbstractAnyLogic.java
index 7a6cfdd..ed2a865 100644
--- 
a/core/logic/src/main/java/org/apache/syncope/core/logic/AbstractAnyLogic.java
+++ 
b/core/logic/src/main/java/org/apache/syncope/core/logic/AbstractAnyLogic.java
@@ -28,7 +28,7 @@ import org.apache.syncope.common.lib.mod.AnyMod;
 import org.apache.syncope.common.lib.to.AnyTO;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.core.misc.RealmUtils;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import org.apache.syncope.core.persistence.api.dao.search.OrderByClause;
 import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
 
@@ -78,7 +78,7 @@ public abstract class AbstractAnyLogic<TO extends AnyTO, MOD 
extends AnyMod>
             }
         })) {
 
-            throw new UnauthorizedException(
+            throw new DelegatedAdministrationException(
                     this instanceof UserLogic
                             ? AnyTypeKind.USER
                             : this instanceof GroupLogic

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/logic/src/main/java/org/apache/syncope/core/logic/GroupLogic.java
----------------------------------------------------------------------
diff --git 
a/core/logic/src/main/java/org/apache/syncope/core/logic/GroupLogic.java 
b/core/logic/src/main/java/org/apache/syncope/core/logic/GroupLogic.java
index 1c3d7f5..5f7cf6f 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/GroupLogic.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/GroupLogic.java
@@ -51,7 +51,7 @@ import 
org.apache.syncope.core.provisioning.api.data.GroupDataBinder;
 import org.apache.syncope.core.provisioning.api.propagation.PropagationManager;
 import 
org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor;
 import org.apache.syncope.core.misc.security.AuthContextUtils;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
 import org.apache.syncope.core.provisioning.api.AnyTransformer;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -103,7 +103,7 @@ public class GroupLogic extends AbstractAnyLogic<GroupTO, 
GroupMod> {
             }
         })) {
 
-            throw new UnauthorizedException(AnyTypeKind.GROUP, key);
+            throw new DelegatedAdministrationException(AnyTypeKind.GROUP, key);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/logic/src/main/java/org/apache/syncope/core/logic/UserLogic.java
----------------------------------------------------------------------
diff --git 
a/core/logic/src/main/java/org/apache/syncope/core/logic/UserLogic.java 
b/core/logic/src/main/java/org/apache/syncope/core/logic/UserLogic.java
index c8a986d..aab476a 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/UserLogic.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/UserLogic.java
@@ -19,7 +19,6 @@
 package org.apache.syncope.core.logic;
 
 import java.lang.reflect.Method;
-import java.security.AccessControlException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -220,11 +219,7 @@ public class UserLogic extends AbstractAnyLogic<UserTO, 
UserMod> {
     @PreAuthorize("isAuthenticated() and not(hasRole('" + 
Entitlement.ANONYMOUS + "'))")
     public UserTO selfUpdate(final UserMod userMod) {
         UserTO userTO = binder.getAuthenticatedUserTO();
-
-        if (userTO.getKey() != userMod.getKey()) {
-            throw new AccessControlException("Not allowed for user with key " 
+ userMod.getKey());
-        }
-
+        userMod.setKey(userTO.getKey());
         return doUpdate(userMod);
     }
 
@@ -298,6 +293,13 @@ public class UserLogic extends AbstractAnyLogic<UserTO, 
UserMod> {
         return savedTO;
     }
 
+    @PreAuthorize("hasRole('" + Entitlement.MUST_CHANGE_PASSWORD + "')")
+    public UserTO changePassword(final String password) {
+        UserMod userMod = new UserMod();
+        userMod.setPassword(password);
+        return selfUpdate(userMod);
+    }
+
     @PreAuthorize("isAnonymous() or hasRole('" + Entitlement.ANONYMOUS + "')")
     @Transactional
     public void requestPasswordReset(final String username, final String 
securityAnswer) {

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/java/org/apache/syncope/core/misc/security/AuthDataAccessor.java
----------------------------------------------------------------------
diff --git 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/AuthDataAccessor.java
 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/AuthDataAccessor.java
index f6324a1..ff9e646 100644
--- 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/AuthDataAccessor.java
+++ 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/AuthDataAccessor.java
@@ -28,8 +28,6 @@ import java.util.Set;
 import javax.annotation.Resource;
 import org.apache.commons.collections4.Closure;
 import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.collections4.IteratorUtils;
-import org.apache.commons.collections4.PredicateUtils;
 import org.apache.commons.collections4.SetUtils;
 import org.apache.commons.collections4.Transformer;
 import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -232,53 +230,55 @@ public class AuthDataAccessor {
         if (anonymousUser.equals(username)) {
             authorities.add(new 
SyncopeGrantedAuthority(Entitlement.ANONYMOUS));
         } else if (adminUser.equals(username)) {
-            
CollectionUtils.collect(IteratorUtils.filteredIterator(Entitlement.values().iterator(),
-                    
PredicateUtils.notPredicate(PredicateUtils.equalPredicate(Entitlement.ANONYMOUS))),
-                    new Transformer<String, SyncopeGrantedAuthority>() {
+            CollectionUtils.collect(Entitlement.values(), new 
Transformer<String, SyncopeGrantedAuthority>() {
 
-                        @Override
-                        public SyncopeGrantedAuthority transform(final String 
entitlement) {
-                            return new SyncopeGrantedAuthority(entitlement, 
SyncopeConstants.ROOT_REALM);
-                        }
-                    },
-                    authorities);
+                @Override
+                public SyncopeGrantedAuthority transform(final String 
entitlement) {
+                    return new SyncopeGrantedAuthority(entitlement, 
SyncopeConstants.ROOT_REALM);
+                }
+            }, authorities);
         } else {
             User user = userDAO.find(username);
             if (user == null) {
                 throw new UsernameNotFoundException("Could not find any user 
with id " + username);
             }
 
-            // Give entitlements as assigned by roles (with realms, where 
applicable) - assigned either
-            // statically and dynamically
-            for (final Role role : userDAO.findAllRoles(user)) {
-                CollectionUtils.forAllDo(role.getEntitlements(), new 
Closure<String>() {
+            if (user.isMustChangePassword()) {
+                authorities.add(new 
SyncopeGrantedAuthority(Entitlement.MUST_CHANGE_PASSWORD));
+            } else {
+                // Give entitlements as assigned by roles (with realms, where 
applicable) - assigned either
+                // statically and dynamically
+                for (final Role role : userDAO.findAllRoles(user)) {
+                    CollectionUtils.forAllDo(role.getEntitlements(), new 
Closure<String>() {
 
-                    @Override
-                    public void execute(final String entitlement) {
-                        SyncopeGrantedAuthority authority = new 
SyncopeGrantedAuthority(entitlement);
-                        authorities.add(authority);
+                        @Override
+                        public void execute(final String entitlement) {
+                            SyncopeGrantedAuthority authority = new 
SyncopeGrantedAuthority(entitlement);
+                            authorities.add(authority);
+
+                            List<String> realmFullPahs = new ArrayList<>();
+                            CollectionUtils.collect(role.getRealms(), new 
Transformer<Realm, String>() {
+
+                                @Override
+                                public String transform(final Realm realm) {
+                                    return realm.getFullPath();
+                                }
+                            }, realmFullPahs);
+                            authority.addRealms(realmFullPahs);
+                        }
+                    });
+                }
 
-                        List<String> realmFullPahs = new ArrayList<>();
-                        CollectionUtils.collect(role.getRealms(), new 
Transformer<Realm, String>() {
+                // Give group entitlements for owned groups
+                for (Group group : groupDAO.findOwnedByUser(user.getKey())) {
+                    for (String entitlement : Arrays.asList(
+                            Entitlement.GROUP_READ, Entitlement.GROUP_UPDATE, 
Entitlement.GROUP_DELETE)) {
 
-                            @Override
-                            public String transform(final Realm realm) {
-                                return realm.getFullPath();
-                            }
-                        }, realmFullPahs);
-                        authority.addRealms(realmFullPahs);
+                        SyncopeGrantedAuthority authority = new 
SyncopeGrantedAuthority(entitlement);
+                        authority.addRealm(
+                                
RealmUtils.getGroupOwnerRealm(group.getRealm().getFullPath(), group.getKey()));
+                        authorities.add(authority);
                     }
-                });
-            }
-
-            // Give group entitlements for owned groups
-            for (Group group : groupDAO.findOwnedByUser(user.getKey())) {
-                for (String entitlement : Arrays.asList(
-                        Entitlement.GROUP_READ, Entitlement.GROUP_UPDATE, 
Entitlement.GROUP_DELETE)) {
-
-                    SyncopeGrantedAuthority authority = new 
SyncopeGrantedAuthority(entitlement);
-                    
authority.addRealm(RealmUtils.getGroupOwnerRealm(group.getRealm().getFullPath(),
 group.getKey()));
-                    authorities.add(authority);
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/java/org/apache/syncope/core/misc/security/DelegatedAdministrationException.java
----------------------------------------------------------------------
diff --git 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/DelegatedAdministrationException.java
 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/DelegatedAdministrationException.java
new file mode 100644
index 0000000..13a449f
--- /dev/null
+++ 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/DelegatedAdministrationException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.syncope.core.misc.security;
+
+import org.apache.syncope.common.lib.types.AnyTypeKind;
+
+public class DelegatedAdministrationException extends RuntimeException {
+
+    private static final long serialVersionUID = 7540587364235915081L;
+
+    public DelegatedAdministrationException(final AnyTypeKind type, final Long 
key) {
+        super("Missing entitlement or realm administration for "
+                + (key == null
+                        ? "new " + type
+                        : type + " " + key));
+    }
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/java/org/apache/syncope/core/misc/security/MustChangePasswordFilter.java
----------------------------------------------------------------------
diff --git 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/MustChangePasswordFilter.java
 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/MustChangePasswordFilter.java
new file mode 100644
index 0000000..3aafb47
--- /dev/null
+++ 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/MustChangePasswordFilter.java
@@ -0,0 +1,80 @@
+/*
+ * 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.syncope.core.misc.security;
+
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.Predicate;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.syncope.common.lib.types.Entitlement;
+import org.springframework.security.access.AccessDeniedException;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
+import 
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
+
+public class MustChangePasswordFilter implements Filter {
+
+    private static final String[] ALLOWED = new String[] {
+        "/users/self", "/users/self/changePassword"
+    };
+
+    @Override
+    public void init(final FilterConfig filterConfig) throws ServletException {
+        // not used
+    }
+
+    @Override
+    public void destroy() {
+        // not used
+    }
+
+    @Override
+    public void doFilter(final ServletRequest request, final ServletResponse 
response, final FilterChain chain)
+            throws IOException, ServletException {
+
+        if (request instanceof SecurityContextHolderAwareRequestWrapper) {
+            boolean isMustChangePassword = CollectionUtils.exists(
+                    
SecurityContextHolder.getContext().getAuthentication().getAuthorities(),
+                    new Predicate<GrantedAuthority>() {
+
+                        @Override
+                        public boolean evaluate(final GrantedAuthority 
authority) {
+                            return 
Entitlement.MUST_CHANGE_PASSWORD.equals(authority.getAuthority());
+                        }
+                    });
+
+            SecurityContextHolderAwareRequestWrapper wrapper =
+                    
SecurityContextHolderAwareRequestWrapper.class.cast(request);
+            if (isMustChangePassword && 
"GET".equalsIgnoreCase(wrapper.getMethod())
+                    && !ArrayUtils.contains(ALLOWED, wrapper.getPathInfo())) {
+
+                throw new AccessDeniedException("Please change your password 
first");
+            }
+        }
+
+        chain.doFilter(request, response);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAccessDeniedHandler.java
----------------------------------------------------------------------
diff --git 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAccessDeniedHandler.java
 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAccessDeniedHandler.java
new file mode 100644
index 0000000..8ab1d31
--- /dev/null
+++ 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAccessDeniedHandler.java
@@ -0,0 +1,43 @@
+/*
+ * 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.syncope.core.misc.security;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.syncope.common.rest.api.RESTHeaders;
+import org.springframework.security.access.AccessDeniedException;
+import org.springframework.security.web.access.AccessDeniedHandlerImpl;
+
+/**
+ * Render Spring's {@link AccessDeniedException} as other Syncope errors.
+ */
+public class SyncopeAccessDeniedHandler extends AccessDeniedHandlerImpl {
+
+    @Override
+    public void handle(final HttpServletRequest request, final 
HttpServletResponse response,
+            final AccessDeniedException accessDeniedException) throws 
IOException, ServletException {
+
+        response.addHeader(RESTHeaders.ERROR_INFO, 
accessDeniedException.getMessage());
+
+        super.handle(request, response, accessDeniedException);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAuthenticationEntryPoint.java
----------------------------------------------------------------------
diff --git 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAuthenticationEntryPoint.java
 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAuthenticationEntryPoint.java
new file mode 100644
index 0000000..6213264
--- /dev/null
+++ 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/SyncopeAuthenticationEntryPoint.java
@@ -0,0 +1,43 @@
+/*
+ * 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.syncope.core.misc.security;
+
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.syncope.common.rest.api.RESTHeaders;
+import org.springframework.security.core.AuthenticationException;
+import 
org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
+
+/**
+ * Render Spring's {@link AuthenticationException} as other Syncope errors.
+ */
+public class SyncopeAuthenticationEntryPoint extends 
BasicAuthenticationEntryPoint {
+
+    @Override
+    public void commence(final HttpServletRequest request, final 
HttpServletResponse response,
+            final AuthenticationException authException) throws IOException, 
ServletException {
+
+        response.addHeader(RESTHeaders.ERROR_INFO, authException.getMessage());
+
+        super.commence(request, response, authException);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/java/org/apache/syncope/core/misc/security/UnauthorizedException.java
----------------------------------------------------------------------
diff --git 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/UnauthorizedException.java
 
b/core/misc/src/main/java/org/apache/syncope/core/misc/security/UnauthorizedException.java
deleted file mode 100644
index b2054cb..0000000
--- 
a/core/misc/src/main/java/org/apache/syncope/core/misc/security/UnauthorizedException.java
+++ /dev/null
@@ -1,33 +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.syncope.core.misc.security;
-
-import org.apache.syncope.common.lib.types.AnyTypeKind;
-
-public class UnauthorizedException extends RuntimeException {
-
-    private static final long serialVersionUID = 7540587364235915081L;
-
-    public UnauthorizedException(final AnyTypeKind type, final Long key) {
-        super("Missing entitlement or realm administration for "
-                + (key == null
-                        ? "new " + type
-                        : type + " " + key));
-    }
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/misc/src/main/resources/securityContext.xml
----------------------------------------------------------------------
diff --git a/core/misc/src/main/resources/securityContext.xml 
b/core/misc/src/main/resources/securityContext.xml
index 711e2f3..222d64a 100644
--- a/core/misc/src/main/resources/securityContext.xml
+++ b/core/misc/src/main/resources/securityContext.xml
@@ -53,12 +53,27 @@ under the License.
   <bean id="syncopeAuthenticationDetailsSource"
         
class="org.apache.syncope.core.misc.security.SyncopeAuthenticationDetailsSource"/>
 
-  <security:http security-context-repository-ref="securityContextRepository" 
realm="Apache Syncope authentication" 
+  <bean id="mustChangePasswordFilter" 
class="org.apache.syncope.core.misc.security.MustChangePasswordFilter"/>
+      
+  <bean id="syncopeAuthenticationEntryPoint" 
+        
class="org.apache.syncope.core.misc.security.SyncopeAuthenticationEntryPoint">
+    <property name="realmName" value="Apache Syncope authentication"/>
+  </bean>
+
+  <bean id="syncopeAccessDeniedHandler" 
class="org.apache.syncope.core.misc.security.SyncopeAccessDeniedHandler"/>
+  
+  <security:http security-context-repository-ref="securityContextRepository"
                  use-expressions="false" disable-url-rewriting="false">
-    <security:http-basic 
authentication-details-source-ref="syncopeAuthenticationDetailsSource"/>
+
+    <security:http-basic entry-point-ref="syncopeAuthenticationEntryPoint"
+                         
authentication-details-source-ref="syncopeAuthenticationDetailsSource"/>
     <security:anonymous username="${anonymousUser}"/>
     <security:intercept-url pattern="/**"/>
     
+    <security:custom-filter before="FILTER_SECURITY_INTERCEPTOR" 
ref="mustChangePasswordFilter"/>
+    
+    <security:access-denied-handler ref="syncopeAccessDeniedHandler"/>
+    
     <security:headers disabled="true"/>
     <security:csrf disabled="true"/>
   </security:http>

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/user/User.java
----------------------------------------------------------------------
diff --git 
a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/user/User.java
 
b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/user/User.java
index c3b0ca6..f3da581 100644
--- 
a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/user/User.java
+++ 
b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/user/User.java
@@ -87,6 +87,10 @@ public interface User extends Any<UPlainAttr, UDerAttr, 
UVirAttr> {
 
     void setSuspended(Boolean suspended);
 
+    boolean isMustChangePassword();
+
+    void setMustChangePassword(boolean mustChangePassword);
+
     @Override
     boolean add(UPlainAttr attr);
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnyObjectDAO.java
----------------------------------------------------------------------
diff --git 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnyObjectDAO.java
 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnyObjectDAO.java
index 75b903d..f5d45b9 100644
--- 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnyObjectDAO.java
+++ 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnyObjectDAO.java
@@ -31,7 +31,7 @@ import org.apache.commons.collections4.Transformer;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.Entitlement;
 import org.apache.syncope.core.misc.security.AuthContextUtils;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
 import org.apache.syncope.core.persistence.api.dao.GroupDAO;
 import org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond;
@@ -75,7 +75,7 @@ public class JPAAnyObjectDAO extends 
AbstractAnyDAO<AnyObject> implements AnyObj
             }
         });
         if (authRealms == null || authRealms.isEmpty() || !authorized) {
-            throw new UnauthorizedException(AnyTypeKind.ANY_OBJECT, 
anyObject.getKey());
+            throw new DelegatedAdministrationException(AnyTypeKind.ANY_OBJECT, 
anyObject.getKey());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAGroupDAO.java
----------------------------------------------------------------------
diff --git 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAGroupDAO.java
 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAGroupDAO.java
index 368ea07..652c1f7 100644
--- 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAGroupDAO.java
+++ 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAGroupDAO.java
@@ -41,7 +41,7 @@ import 
org.apache.syncope.common.lib.types.PropagationByResource;
 import org.apache.syncope.core.misc.RealmUtils;
 import org.apache.syncope.core.misc.search.SearchCondConverter;
 import org.apache.syncope.core.misc.security.AuthContextUtils;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
 import org.apache.syncope.core.persistence.api.entity.Any;
 import org.apache.syncope.core.persistence.api.entity.AnyUtils;
@@ -82,7 +82,7 @@ public class JPAGroupDAO extends AbstractAnyDAO<Group> 
implements GroupDAO {
             }
         });
         if (authRealms == null || authRealms.isEmpty() || !authorized) {
-            throw new UnauthorizedException(AnyTypeKind.GROUP, group.getKey());
+            throw new DelegatedAdministrationException(AnyTypeKind.GROUP, 
group.getKey());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAUserDAO.java
----------------------------------------------------------------------
diff --git 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAUserDAO.java
 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAUserDAO.java
index bdc552e..b8f2c47 100644
--- 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAUserDAO.java
+++ 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAUserDAO.java
@@ -39,7 +39,7 @@ import 
org.apache.syncope.common.lib.types.EntityViolationType;
 import org.apache.syncope.core.misc.policy.AccountPolicyException;
 import org.apache.syncope.core.misc.policy.PasswordPolicyException;
 import org.apache.syncope.core.misc.security.AuthContextUtils;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import org.apache.syncope.core.misc.spring.ApplicationContextProvider;
 import org.apache.syncope.core.persistence.api.ImplementationLookup;
 import 
org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
@@ -112,7 +112,7 @@ public class JPAUserDAO extends AbstractAnyDAO<User> 
implements UserDAO {
                 }
             });
             if (authRealms == null || authRealms.isEmpty() || !authorized) {
-                throw new UnauthorizedException(AnyTypeKind.USER, 
user.getKey());
+                throw new DelegatedAdministrationException(AnyTypeKind.USER, 
user.getKey());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/user/JPAUser.java
----------------------------------------------------------------------
diff --git 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/user/JPAUser.java
 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/user/JPAUser.java
index 1f153da..e7a688c 100644
--- 
a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/user/JPAUser.java
+++ 
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/user/JPAUser.java
@@ -94,7 +94,7 @@ public class JPAUser extends AbstractAny<UPlainAttr, 
UDerAttr, UVirAttr> impleme
             @JoinColumn(name = "user_id"),
             inverseJoinColumns =
             @JoinColumn(name = "role_id"))
-    private List<JPARole> roles;
+    private List<JPARole> roles = new ArrayList<>();
 
     @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
     @Valid
@@ -127,13 +127,13 @@ public class JPAUser extends AbstractAny<UPlainAttr, 
UDerAttr, UVirAttr> impleme
     @Column(name = "passwordHistoryValue")
     @CollectionTable(name = "SyncopeUser_passwordHistory", joinColumns =
             @JoinColumn(name = "user_id", referencedColumnName = "id"))
-    private List<String> passwordHistory;
+    private List<String> passwordHistory = new ArrayList<>();
 
     /**
      * Subsequent failed logins.
      */
     @Column(nullable = true)
-    private Integer failedLogins;
+    private Integer failedLogins = 0;
 
     /**
      * Username/Login.
@@ -159,7 +159,12 @@ public class JPAUser extends AbstractAny<UPlainAttr, 
UDerAttr, UVirAttr> impleme
     @Basic
     @Min(0)
     @Max(1)
-    private Integer suspended;
+    private Integer suspended = getBooleanAsInteger(Boolean.FALSE);
+
+    @Basic
+    @Min(0)
+    @Max(1)
+    private Integer mustChangePassword = getBooleanAsInteger(Boolean.FALSE);
 
     /**
      * Provisioning external resources.
@@ -193,16 +198,6 @@ public class JPAUser extends AbstractAny<UPlainAttr, 
UDerAttr, UVirAttr> impleme
     @Column(nullable = true)
     private String securityAnswer;
 
-    public JPAUser() {
-        super();
-
-        roles = new ArrayList<>();
-        memberships = new ArrayList<>();
-        passwordHistory = new ArrayList<>();
-        failedLogins = 0;
-        suspended = getBooleanAsInteger(Boolean.FALSE);
-    }
-
     @Override
     public Long getKey() {
         return id;
@@ -265,6 +260,7 @@ public class JPAUser extends AbstractAny<UPlainAttr, 
UDerAttr, UVirAttr> impleme
 
         this.password = password;
         this.cipherAlgorithm = cipherAlgoritm;
+        setMustChangePassword(false);
     }
 
     @Override
@@ -471,6 +467,16 @@ public class JPAUser extends AbstractAny<UPlainAttr, 
UDerAttr, UVirAttr> impleme
     }
 
     @Override
+    public void setMustChangePassword(final boolean mustChangePassword) {
+        this.mustChangePassword = getBooleanAsInteger(mustChangePassword);
+    }
+
+    @Override
+    public boolean isMustChangePassword() {
+        return isBooleanAsInteger(mustChangePassword);
+    }
+
+    @Override
     public boolean verifyPasswordHistory(final String password, final int 
size) {
         boolean res = false;
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
index 2e1224b..5589fa8 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
@@ -128,7 +128,7 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
 
     private void setPassword(final User user, final String password, final 
SyncopeClientCompositeException scce) {
         try {
-            final String algorithm = confDAO.find(
+            String algorithm = confDAO.find(
                     "password.cipher.algorithm", 
CipherAlgorithm.AES.name()).getValues().get(0).getStringValue();
             CipherAlgorithm predefined = CipherAlgorithm.valueOf(algorithm);
             user.setPassword(password, predefined);
@@ -223,6 +223,8 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
             }
         }
         user.setSecurityAnswer(userTO.getSecurityAnswer());
+
+        user.setMustChangePassword(userTO.isMustChangePassword());
     }
 
     private boolean isPasswordMapped(final ExternalResource resource) {
@@ -293,6 +295,8 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
             }
         }
 
+        user.setMustChangePassword(userMod.isMustChangePassword());
+
         // roles
         CollectionUtils.forAllDo(userMod.getRolesToRemove(), new 
Closure<Long>() {
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/sync/AbstractSyncResultHandler.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/sync/AbstractSyncResultHandler.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/sync/AbstractSyncResultHandler.java
index b0b5a58..c69a0d6 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/sync/AbstractSyncResultHandler.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/sync/AbstractSyncResultHandler.java
@@ -33,7 +33,7 @@ import 
org.apache.syncope.core.persistence.api.dao.NotFoundException;
 import org.apache.syncope.core.persistence.api.entity.task.SyncTask;
 import 
org.apache.syncope.core.provisioning.api.propagation.PropagationException;
 import org.apache.syncope.core.provisioning.api.sync.SyncActions;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import org.apache.syncope.core.persistence.api.entity.AnyUtils;
 import org.apache.syncope.core.persistence.api.entity.resource.Provision;
 import org.apache.syncope.core.provisioning.api.AnyTransformer;
@@ -572,7 +572,7 @@ public abstract class AbstractSyncResultHandler extends 
AbstractSyncopeResultHan
                 delResults.add(result);
             } catch (NotFoundException e) {
                 LOG.error("Could not find {} {}", 
provision.getAnyType().getKey(), id, e);
-            } catch (UnauthorizedException e) {
+            } catch (DelegatedAdministrationException e) {
                 LOG.error("Not allowed to read {} {}", 
provision.getAnyType().getKey(), id, e);
             } catch (Exception e) {
                 LOG.error("Could not delete {} {}", 
provision.getAnyType().getKey(), id, e);

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java
----------------------------------------------------------------------
diff --git 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java
 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java
index 3307f66..2de8989 100644
--- 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java
+++ 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java
@@ -45,7 +45,7 @@ import org.apache.syncope.common.lib.to.ErrorTO;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.lib.types.EntityViolationType;
 import org.apache.syncope.common.rest.api.RESTHeaders;
-import org.apache.syncope.core.misc.security.UnauthorizedException;
+import org.apache.syncope.core.misc.security.DelegatedAdministrationException;
 import 
org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
 import 
org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException;
 import org.apache.syncope.core.persistence.api.dao.DuplicateException;
@@ -71,8 +71,6 @@ import 
org.springframework.transaction.TransactionSystemException;
 @Provider
 public class RestServiceExceptionMapper implements ExceptionMapper<Exception>, 
ResponseExceptionMapper<Exception> {
 
-    private static final String BASIC_REALM_UNAUTHORIZED = "Basic 
realm=\"Apache Syncope authentication\"";
-
     private static final Logger LOG = 
LoggerFactory.getLogger(RestServiceExceptionMapper.class);
 
     private final ValidationExceptionMapper validationEM = new 
ValidationExceptionMapper();
@@ -111,10 +109,11 @@ public class RestServiceExceptionMapper implements 
ExceptionMapper<Exception>, R
 
             builder = builder(response).entity(error);
         } else if (ex instanceof AccessDeniedException) {
-            builder = Response.status(Response.Status.UNAUTHORIZED).
-                    header(HttpHeaders.WWW_AUTHENTICATE, 
BASIC_REALM_UNAUTHORIZED);
-        } else if (ex instanceof UnauthorizedException) {
-            builder = builder(ClientExceptionType.Unauthorized, 
ExceptionUtils.getRootCauseMessage(ex));
+            builder = Response.status(Response.Status.FORBIDDEN).
+                    header(RESTHeaders.ERROR_CODE, 
Response.Status.FORBIDDEN.getReasonPhrase()).
+                    header(RESTHeaders.ERROR_INFO, ex.getMessage());
+        } else if (ex instanceof DelegatedAdministrationException) {
+            builder = builder(ClientExceptionType.DelegatedAdministration, 
ExceptionUtils.getRootCauseMessage(ex));
         } else if (ex instanceof UnallowedSchemaException) {
             builder = builder(ClientExceptionType.UnallowedSchemas, 
ExceptionUtils.getRootCauseMessage(ex));
         } else if (ex instanceof EntityExistsException || ex instanceof 
DuplicateException) {

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserSelfServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserSelfServiceImpl.java
 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserSelfServiceImpl.java
index 4930bbb..b1fbc35 100644
--- 
a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserSelfServiceImpl.java
+++ 
b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/UserSelfServiceImpl.java
@@ -43,7 +43,7 @@ public class UserSelfServiceImpl extends AbstractServiceImpl 
implements UserSelf
     @Override
     public Response create(final UserTO userTO, final boolean storePassword) {
         if (!syncopeLogic.isSelfRegAllowed()) {
-            SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.Unauthorized);
+            SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.DelegatedAdministration);
             sce.getElements().add("Self registration forbidden by 
configuration");
             throw sce;
         }
@@ -75,9 +75,15 @@ public class UserSelfServiceImpl extends AbstractServiceImpl 
implements UserSelf
     }
 
     @Override
+    public Response changePassword(final String password) {
+        UserTO updated = logic.changePassword(password);
+        return modificationResponse(updated);
+    }
+
+    @Override
     public void requestPasswordReset(final String username, final String 
securityAnswer) {
         if (!syncopeLogic.isPwdResetAllowed()) {
-            SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.Unauthorized);
+            SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.DelegatedAdministration);
             sce.getElements().add("Password reset forbidden by configuration");
             throw sce;
         }
@@ -88,7 +94,7 @@ public class UserSelfServiceImpl extends AbstractServiceImpl 
implements UserSelf
     @Override
     public void confirmPasswordReset(final String token, final String 
password) {
         if (!syncopeLogic.isPwdResetAllowed()) {
-            SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.Unauthorized);
+            SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.DelegatedAdministration);
             sce.getElements().add("Password reset forbidden by configuration");
             throw sce;
         }

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AuthenticationITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AuthenticationITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AuthenticationITCase.java
index cdfd1b1..79ec47a 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AuthenticationITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/AuthenticationITCase.java
@@ -101,7 +101,7 @@ public class AuthenticationITCase extends AbstractITCase {
 
         // 3. as admin
         self = adminClient.self();
-        assertEquals(Entitlement.values().size() - 1, self.getKey().size());
+        assertEquals(Entitlement.values().size(), self.getKey().size());
         assertFalse(self.getKey().keySet().contains(Entitlement.ANONYMOUS));
         assertEquals(ADMIN_UNAME, self.getValue().getUsername());
 
@@ -184,7 +184,7 @@ public class AuthenticationITCase extends AbstractITCase {
             fail();
         } catch (SyncopeClientException e) {
             assertNotNull(e);
-            assertEquals(ClientExceptionType.Unauthorized, e.getType());
+            assertEquals(ClientExceptionType.DelegatedAdministration, 
e.getType());
         }
     }
 
@@ -268,7 +268,7 @@ public class AuthenticationITCase extends AbstractITCase {
                 delegatedUserService.create(user);
                 fail();
             } catch (SyncopeClientException e) {
-                assertEquals(ClientExceptionType.Unauthorized, e.getType());
+                assertEquals(ClientExceptionType.DelegatedAdministration, 
e.getType());
             }
 
             // 5. set realm to /even/two -> succeed
@@ -291,7 +291,7 @@ public class AuthenticationITCase extends AbstractITCase {
                 delegatedUserService.update(userMod);
                 fail();
             } catch (SyncopeClientException e) {
-                assertEquals(ClientExceptionType.Unauthorized, e.getType());
+                assertEquals(ClientExceptionType.DelegatedAdministration, 
e.getType());
             }
 
             // 6. revert realm change -> succeed

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/GroupITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/GroupITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/GroupITCase.java
index 7fece69..6ad53c7 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/GroupITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/GroupITCase.java
@@ -181,7 +181,7 @@ public class GroupITCase extends AbstractITCase {
             groupService2.read(3L);
             fail();
         } catch (SyncopeClientException e) {
-            assertEquals(ClientExceptionType.Unauthorized, e.getType());
+            assertEquals(ClientExceptionType.DelegatedAdministration, 
e.getType());
         }
 
         List<GroupTO> groups = groupService2.own();

http://git-wip-us.apache.org/repos/asf/syncope/blob/e9839b35/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/UserSelfITCase.java
----------------------------------------------------------------------
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/UserSelfITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/UserSelfITCase.java
index 78eb504..d356afc 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/UserSelfITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/reference/UserSelfITCase.java
@@ -47,6 +47,7 @@ import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.rest.api.Preference;
 import org.apache.syncope.common.rest.api.RESTHeaders;
+import org.apache.syncope.common.rest.api.service.ResourceService;
 import org.apache.syncope.common.rest.api.service.UserSelfService;
 import org.apache.syncope.common.rest.api.service.UserService;
 import org.junit.Assume;
@@ -351,4 +352,35 @@ public class UserSelfITCase extends AbstractITCase {
         configurationService.set(attrTO("passwordReset.securityQuestion", 
"true"));
     }
 
+    @Test
+    public void mustChangePassword() {
+        // 0. access as vivaldi -> succeed
+        SyncopeClient vivaldiClient = clientFactory.create("vivaldi", 
"password");
+        Pair<Map<String, Set<String>>, UserTO> self = vivaldiClient.self();
+        assertFalse(self.getRight().isMustChangePassword());
+
+        // 1. update user vivaldi (3) requirig password update
+        UserMod userMod = new UserMod();
+        userMod.setKey(3L);
+        userMod.setMustChangePassword(true);
+        UserTO vivaldi = updateUser(userMod);
+        assertTrue(vivaldi.isMustChangePassword());
+
+        // 2. attempt to access -> fail
+        try {
+            vivaldiClient.getService(ResourceService.class).list();
+            fail();
+        } catch (AccessControlException e) {
+            assertNotNull(e);
+            assertEquals("Please change your password first", e.getMessage());
+        }
+
+        // 3. change password
+        
vivaldiClient.getService(UserSelfService.class).changePassword("password123");
+
+        // 4. verify it worked
+        self = clientFactory.create("vivaldi", "password123").self();
+        assertFalse(self.getRight().isMustChangePassword());
+    }
+
 }

Reply via email to