[fix]AccountsRest/LoginRestの例外処理をreturnに修正 #102

Merged r-kuraoka merged 1 commit into nitta-lab-2021:master from nitta-lab-2021:fix/uid_bug_fix on 25 May 2021
Showing 4 changed files
View
1
■■■■
src/main/java/org/ntlab/acanthus_server/entities/Account.java
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.ntlab.acanthus_server.models.Gallery;
 
import java.util.HashMap;
import java.util.UUID;
 
View
4
src/main/java/org/ntlab/acanthus_server/models/Accounts.java
* @param password パスワード
*/
public Account registerAccount(String name, String email, String password) {
var uid = new Random().nextInt();
 
// uidが被ったらuidの振り直し
while(getAccountByUid(uid) != null) uid = new Random().nextInt();
 
var newAccount = new Account (uid, name, email, password);
accountHashMap.put(uid, newAccount);
 
return newAccount;
View
14
src/main/java/org/ntlab/acanthus_server/resources/accounts/AccountsRest.java
@Produces(MediaType.APPLICATION_JSON)
public Collection<AccountJson> getAccounts(@QueryParam("name") String name) {
 
var accountJsonList = new ArrayList<AccountJson>();
 
Consumer<Account> addJson = (account) -> {
{
var newAccountJson = new AccountJson(account);
accountJsonList.add(newAccountJson);
response = Response.status(404).entity("該当アカウントが存在しません。");
throw new WebApplicationException(response.build());
}
 
// トークンの検証
// トークンを検証し, 発見したユーザーを返却
if (!token.equals(searchAccount.getToken())) response = Response.status(400).entity("トークンが違います。");
else response.status(200).entity(searchAccount);
else return searchAccount;
 
throw new WebApplicationException(response.build());
}
 
//-----------------------------------------------------------------
// POST
//-----------------------------------------------------------------
// アカウントの新規作成
// Uidを返却する
 
/**
* @param name ユーザー名
* @param email メアド
 
ResponseBuilder response = Response.status(0);
 
// パスワード, メアドが正しく入力されているかチェック
if (!isCorrectName(name) || !isCorrectPassword(password) || !isCorrectEmailAddress(email)) {
if (!isCorrectName(name) || !isCorrectPassword(password) || !isCorrectEmailAddress(email))
response.status(401).entity("入力に誤りがあります。");
throw new WebApplicationException(response.build());
}
 
// すでに同じメールアドレスが存在しているか
var existAccount = accounts.getAccountByEmail(email);
if (existAccount != null) response.status(400).entity("入力されたメールアドレスは既に使われています。");
else {
// アカウント登録
// アカウント登録をしてuidを返す
var newAccount = accounts.registerAccount(name, email, password);
response.status(200).entity(newAccount.getUid());
return newAccount.getUid();
}
 
throw new WebApplicationException(response.build());
}
View
6
src/main/java/org/ntlab/acanthus_server/resources/accounts/LoginRest.java
 
private Accounts accounts = Accounts.getInstance();
 
//-----------------------------------------------------------
 
// ログイン情報が正しいかどうか
/**
* ログイン時のトークン認証
* アカウントが存在して, かつトークンを持っているかを確認する
*/
 
// アカウントとトークンの検証
if (searchAccount == null) response.status(404).entity(false);
if (!token.equals(searchAccount.getToken())) response.status(400).entity(false);
else response.status(200).entity(true);
else return true;
 
throw new WebApplicationException(response.build());
}
 
if (!searchAccount.isMatchedPassword(password)) response.entity(401).entity("不正なパスワードです。");
else {
// トークンをアカウントに設定
searchAccount.updateToken();
response.status(201).entity(searchAccount.getToken());
return searchAccount.getToken();
}
 
throw new WebApplicationException(response.build());
}