diff --git a/src/main/java/org/ntlab/citrusserver/entities/Account.java b/src/main/java/org/ntlab/citrusserver/entities/Account.java index 89d28bf..eb04687 100644 --- a/src/main/java/org/ntlab/citrusserver/entities/Account.java +++ b/src/main/java/org/ntlab/citrusserver/entities/Account.java @@ -28,8 +28,8 @@ } @JsonIgnore public int getBookCount() {return bookcount;} - public void setId(String i) {accountId = i;} + @JsonIgnore public String getId() {return accountId;} public void setPassword(String p) {password = p;} diff --git a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java index 4f5d087..1e76248 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java @@ -82,6 +82,18 @@ } } + // 指定されたIDを変更する (PUT) + public void changeAccountId(String accountId, String newAccountId, String oldPassword, String token) { + if(accountToken.get(accountId).equals(token)) { //token比較 + if(accounts.get(accountId).getPassword().equals(oldPassword)) { //password比較 + + accounts.get(accountId).setId(newAccountId); + accounts.put(newAccountId, accounts.get(accountId)); + accounts.remove(accountId); + } + } + } + // 指定されたIDのパスワードを変更する (PUT) public void changePassword(String accountId, String newPassword, String oldPassword, String token) { if(accountToken.get(accountId).equals(token)) { //token比較 @@ -120,7 +132,7 @@ accounts.get(accountId).setAccountColor(newColor); } } - //////////// + /////////// // アカウントidとパスワードでログインし、tokenを返す (POST) public String login(String accountId, String password) { diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index 1e38ea0..de50990 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -84,6 +84,30 @@ } + //accountのidを変更する(PUT) + @Path("/{account_id}") + @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 + public void changeAccountId(@PathParam("account_id") String accountId, + @FormParam("new_account_id")String newAccountId, + @FormParam("old_password")String oldPassword, + @FormParam("token") String token){ + //404 + if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + //成功 + if(accountManager.checkToken(accountId, token)) { + accountManager.changeAccountId(accountId, newAccountId,oldPassword, token); + return; + } + + //403 + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 + throw new WebApplicationException(response.build()); + } + //指定されたIDのパスワードを変更する (PUT) @Path("/{account_id}/password") @PUT @@ -171,18 +195,6 @@ } /////////////////// - -////////// -// お気に入りの本のbook_idを削除する (DELETE) -// @Path("/{account_id}/favorites/{other_account_id}/{book_id}") -// @DELETE - - // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT) - //@Path("/{account_id}/favorites/{other_account_id}/{book_id}") -// @PUT - - - // アカウントidとパスワードでログインし、tokenを返す (POST) @Path("/{account_id}/login") @POST