package mingzuozhibi.action;

import mingzuozhibi.persist.User;
import mingzuozhibi.support.JsonArg;
import org.json.JSONArray;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController extends BaseController {

    @Transactional
    @GetMapping(value = "/api/admin/users", produces = MEDIA_TYPE)
    public String listAdminUser() {
        JSONArray array = new JSONArray();
        dao.findAll(User.class).forEach(user -> {
            array.put(user.toJSON());
        });

        if (LOGGER.isDebugEnabled()) {
            debugRequest("[size={}]", array.length());
        }
        return objectResult(array);
    }

    @Transactional
    @PostMapping(value = "/api/admin/users", produces = MEDIA_TYPE)
    public String saveAdminUser(
            @JsonArg("$.username") String username,
            @JsonArg("$.password") String password) {
        if (dao.lookup(User.class, "username", username) != null) {
            return errorMessage("用户名已被注册");
        }
        User user = new User(username, password);
        dao.save(user);

        if (LOGGER.isInfoEnabled()) {
            infoRequest("[json={}]", user.toJSON());
        }
        return objectResult(user.toJSON());
    }

    @Transactional
    @PostMapping(value = "/api/admin/users/{id}", produces = MEDIA_TYPE)
    public String editAdminUser(
            @PathVariable("id") Long id,
            @JsonArg("$.username") String username,
            @JsonArg("$.password") String password,
            @JsonArg("$.enabled") boolean enabled) {
        User user = dao.get(User.class, id);

        if (LOGGER.isDebugEnabled()) {
            debugRequest("[Before:{}]", user.toJSON());
        }
        user.setUsername(username);
        user.setEnabled(enabled);
        if (password != null && !password.isEmpty()) {
            user.setPassword(password);
        }

        if (LOGGER.isDebugEnabled()) {
            debugRequest("[Modify:{}]", user.toJSON());
        }
        return objectResult(user.toJSON());
    }

}

@@ -23,16 +23,55 @@
 ction;%0A%0A
+import mingzuozhibi.persist.AutoLogin;%0A
 import m
@@ -469,16 +469,40 @@
 oller;%0A%0A
+import java.util.List;%0A%0A
 @RestCon
@@ -2181,32 +2181,366 @@
 sword(password);
+%0A%0A            List%3CAutoLogin%3E autoLogins = dao.findBy(AutoLogin.class, %22user%22, user);%0A            autoLogins.forEach(autoLogin -%3E %7B%0A                if (LOGGER.isDebugEnabled()) %7B%0A                    LOGGER.debug(%22%5B%E6%B8%85%E7%90%86%E8%87%AA%E5%8A%A8%E7%99%BB%E5%85%A5%E6%95%B0%E6%8D%AE%5D%5BautoLoginId=%7B%7D%5D%22, autoLogin.getId());%0A                %7D%0A                dao.delete(autoLogin);%0A            %7D);
 %0A        %7D%0A%0A    

Clean on modify password