001/* 002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Guillaume Renard <[email protected]> 018 */ 019package org.nuxeo.ecm.restapi.server.jaxrs.usermanager; 020 021import javax.ws.rs.GET; 022import javax.ws.rs.PUT; 023import javax.ws.rs.Path; 024import javax.ws.rs.Produces; 025import javax.ws.rs.core.Context; 026import javax.ws.rs.core.MediaType; 027import javax.ws.rs.core.Request; 028import javax.ws.rs.core.Response; 029import javax.ws.rs.core.Response.Status; 030 031import org.json.JSONException; 032import org.json.JSONObject; 033import org.nuxeo.ecm.core.api.NuxeoPrincipal; 034import org.nuxeo.ecm.platform.usermanager.UserManager; 035import org.nuxeo.ecm.webengine.model.WebObject; 036import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * @since 9.1 041 */ 042@WebObject(type = "me") 043@Produces({ MediaType.APPLICATION_JSON }) 044public class MeObject extends DefaultObject { 045 046 @GET 047 public NuxeoPrincipal doGet(@Context Request request) { 048 return getContext().getCoreSession().getPrincipal(); 049 } 050 051 @PUT 052 @Path("changepassword") 053 public Object changePassword(String payload) throws JSONException { 054 NuxeoPrincipal currentUser = getContext().getCoreSession().getPrincipal(); 055 JSONObject payloadJson = new JSONObject(payload); 056 String oldPassword = payloadJson.getString("oldPassword"); 057 String newPassword = payloadJson.getString("newPassword"); 058 UserManager userManager = Framework.getService(UserManager.class); 059 if (userManager.checkUsernamePassword(currentUser.getName(), oldPassword)) { 060 currentUser.setPassword(newPassword); 061 Framework.doPrivileged(() -> userManager.updateUser(currentUser.getModel())); 062 return currentUser; 063 } else { 064 return Response.status(Status.UNAUTHORIZED).build(); 065 } 066 067 } 068 069}