diff --git a/src/jp/dogrun/ileaflet/controller/login/LogonController.java b/src/jp/dogrun/ileaflet/controller/login/LogonController.java index d4c3a66..6d2804e 100644 --- a/src/jp/dogrun/ileaflet/controller/login/LogonController.java +++ b/src/jp/dogrun/ileaflet/controller/login/LogonController.java @@ -1,5 +1,9 @@ package jp.dogrun.ileaflet.controller.login; +import jp.dogrun.ileaflet.dao.ActorDao; +import jp.dogrun.ileaflet.model.Actor; +import jp.dogrun.ileaflet.util.ApplicationUtil; + import org.slim3.controller.Controller; import org.slim3.controller.Navigation; @@ -9,13 +13,34 @@ public class LogonController extends Controller { public Navigation run() throws Exception { //入力値をMD5化(identityとパスワード) + String identity = requestScope("identity"); + String password = requestScope("password"); //パスワードとユーザから検索 + ActorDao dao = new ActorDao(); + Actor actor = dao.findById(identity); - //存在した場合、 - //指定ページへリダイレクト - + if ( actor == null ) { + //TODO メッセージを付与 + return forward("./index.jsp"); + } + + String target = ApplicationUtil.changeMD5(password); + if ( !target.equals(actor.getPassword()) ) { + //TODO メッセージを付与 + return forward("./index.jsp"); + } + + Integer purchase = actor.getPurchase(); + if ( purchase == null ) { + //TODO メッセージを付与 + return forward("./index.jsp"); + } + + System.out.println("来てるよ?"); - return null; + sessionScope(Actor.class.getName(),actor); + //TODO 指定ページへリダイレクト + return redirect("/dashboard/"); } } diff --git a/src/jp/dogrun/ileaflet/controller/login/RegisterController.java b/src/jp/dogrun/ileaflet/controller/login/RegisterController.java index e0cd61d..4461fac 100644 --- a/src/jp/dogrun/ileaflet/controller/login/RegisterController.java +++ b/src/jp/dogrun/ileaflet/controller/login/RegisterController.java @@ -1,5 +1,17 @@ package jp.dogrun.ileaflet.controller.login; +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.Properties; +import java.util.Random; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + import jp.dogrun.ileaflet.controller.validator.login.RegisterValidators; import jp.dogrun.ileaflet.dao.ActorDao; import jp.dogrun.ileaflet.model.Actor; @@ -7,9 +19,9 @@ import org.slim3.controller.Controller; import org.slim3.controller.Navigation; -import org.slim3.controller.validator.Validators; import org.slim3.datastore.Datastore; -import org.slim3.util.StringUtil; + +import com.google.appengine.api.datastore.Transaction; public class RegisterController extends Controller { @@ -21,22 +33,43 @@ public Navigation run() throws Exception { } Actor actor = createActor(); - Datastore.put(actor); - sessionScope(Actor.class.getName(),actor); - return redirect("/dashboard/"); + + Transaction tx = Datastore.beginTransaction(); + try { + String serverUrl = ApplicationUtil.getHost(request); + String registUrl = serverUrl + "/login/registration?registerCode=" + actor.getKeyword(); + //メールを送信する + sendRegistMail(actor.getName(),actor.getEmail(),registUrl); + Datastore.put(actor); + tx.commit(); + } catch ( Exception ex ) { + tx.rollback(); + throw ex; + } + //TODO メールを送信しましたメッセージ + return forward("../index.jsp"); } private boolean validate() { RegisterValidators v = new RegisterValidators(request); - v.add("identity",v.required()); + v.add("identity",v.required(),v.keyword()); v.add("name",v.required()); - v.add("mail",v.required()); + v.add("mail",v.required(),v.regexp("[\\w\\.\\-]+@(?:[\\w\\-]+\\.)+[\\w\\-]+")); v.add("password",v.required(),v.samePassword()); v.add("actor",v.sameUser()); return v.validate(); } private Actor createActor() { + + String keyword = null; + while (true) { + keyword = createKeyword(); + System.out.println(keyword); + ActorDao dao = new ActorDao(); + Actor actor = dao.findByKeyword(keyword); + if ( actor == null ) break; + } //入力チェック String identity = requestScope("identity"); @@ -50,10 +83,35 @@ private Actor createActor() { actor.setName(name); actor.setEmail(mail); - String enPass = ApplicationUtil.changeMD5(identity + "-" + pass1); + String enPass = ApplicationUtil.changeMD5(pass1); actor.setPassword(enPass); + actor.setPurchase(null); + actor.setKeyword(keyword); return actor; } + private String createKeyword() { + Random r = new Random(); + r.setSeed((new Date()).getTime()); + double xxx = r.nextDouble(); + return ApplicationUtil.changeMD5(String.valueOf(xxx)); + } + + public void sendRegistMail(String name,String email,String url) throws MessagingException, UnsupportedEncodingException { + + InternetAddress ToAddress = new InternetAddress(email,name, "ISO-2022-JP"); + InternetAddress FromAddress = new InternetAddress("secondarykey@gmail.com", "iLeaflet管理者", "ISO-2022-JP"); + + Properties props = new Properties(); + Session session = Session.getDefaultInstance(props, null); + MimeMessage message = new MimeMessage(session); + message.setFrom(FromAddress); + message.addRecipient(Message.RecipientType.TO, ToAddress); + message.setSubject("iLeaflet仮登録のお知らせ", "ISO-2022-JP"); + + message.setText(url); + Transport.send(message); + } + } diff --git a/src/jp/dogrun/ileaflet/controller/login/RegistrationController.java b/src/jp/dogrun/ileaflet/controller/login/RegistrationController.java new file mode 100644 index 0000000..b96fd91 --- /dev/null +++ b/src/jp/dogrun/ileaflet/controller/login/RegistrationController.java @@ -0,0 +1,34 @@ +package jp.dogrun.ileaflet.controller.login; + +import jp.dogrun.ileaflet.dao.ActorDao; +import jp.dogrun.ileaflet.model.Actor; + +import org.slim3.controller.Controller; +import org.slim3.controller.Navigation; +import org.slim3.datastore.Datastore; + +public class RegistrationController extends Controller { + + @Override + public Navigation run() throws Exception { + + String code = requestScope("registerCode"); + ActorDao dao = new ActorDao(); + Actor actor = dao.findByKeyword(code); + if ( actor == null ) { + //TODO 存在しないメッセージ + return forward("index.jsp"); + } + if ( actor.getPurchase() != null ) { + //TODO 既に登録済メッセージ + return forward("index.jsp"); + } + + actor.setPurchase(0); + Datastore.put(actor); + + //セッションに設定する + sessionScope(Actor.class.getName(),actor); + return redirect("/dashboard/"); + } +} diff --git a/src/jp/dogrun/ileaflet/controller/validator/login/KeywordValidator.java b/src/jp/dogrun/ileaflet/controller/validator/login/KeywordValidator.java new file mode 100644 index 0000000..057d10b --- /dev/null +++ b/src/jp/dogrun/ileaflet/controller/validator/login/KeywordValidator.java @@ -0,0 +1,49 @@ +package jp.dogrun.ileaflet.controller.validator.login; + +import java.util.Map; + +import org.slim3.controller.validator.AbstractValidator; +import org.slim3.util.ApplicationMessage; +import org.slim3.util.StringUtil; + +public class KeywordValidator extends AbstractValidator { + + public static KeywordValidator INSTANCE = new KeywordValidator(); + public KeywordValidator() { + super(); + } + + public KeywordValidator(String message) { + super(message); + } + + public String validate(Map parameters, String name) { + String value = (String)parameters.get(name); + if ( isKeyword(value) ) { + if (message != null) { + return message; + } + return ApplicationMessage.get(getMessageKey(), getLabel(name)); + } + return null; + } + + private boolean isKeyword(String value) { + if ( StringUtil.isEmpty(value)) return false; + if( + value.equals("dashboard") || + value.equals("dashboard") || + value.equals("dashboard") + ) { + return true; + } + + return false; + } + + @Override + protected String getMessageKey() { + return "validator.keyword"; + } + +} diff --git a/src/jp/dogrun/ileaflet/controller/validator/login/RegisterValidators.java b/src/jp/dogrun/ileaflet/controller/validator/login/RegisterValidators.java index c014897..7e33266 100644 --- a/src/jp/dogrun/ileaflet/controller/validator/login/RegisterValidators.java +++ b/src/jp/dogrun/ileaflet/controller/validator/login/RegisterValidators.java @@ -24,5 +24,9 @@ public SamePasswordValidator samePassword() { public SameActorValidator sameUser() { return SameActorValidator.INSTANCE; } + + public KeywordValidator keyword() { + return KeywordValidator.INSTANCE; + } } diff --git a/src/jp/dogrun/ileaflet/dao/ActorDao.java b/src/jp/dogrun/ileaflet/dao/ActorDao.java index 86d06c4..7796edd 100644 --- a/src/jp/dogrun/ileaflet/dao/ActorDao.java +++ b/src/jp/dogrun/ileaflet/dao/ActorDao.java @@ -23,6 +23,14 @@ public Actor findByMail(String email) { meta.email.equal(email) ).asSingle(); } + + public Actor findByKeyword(String keyword) { + ActorMeta meta = ActorMeta.get(); + return Datastore.query(Actor.class). + filter( + meta.keyword.equal(keyword) + ).asSingle(); + } } diff --git a/src/jp/dogrun/ileaflet/meta/ActorMeta.java b/src/jp/dogrun/ileaflet/meta/ActorMeta.java index 65ae6fb..c602d71 100644 --- a/src/jp/dogrun/ileaflet/meta/ActorMeta.java +++ b/src/jp/dogrun/ileaflet/meta/ActorMeta.java @@ -1,6 +1,6 @@ package jp.dogrun.ileaflet.meta; -//@javax.annotation.Generated(value = { "slim3-gen", "@VERSION@" }, date = "2013-01-17 18:00:55") +//@javax.annotation.Generated(value = { "slim3-gen", "@VERSION@" }, date = "2013-01-18 18:42:50") /** */ public final class ActorMeta extends org.slim3.datastore.ModelMeta { @@ -19,12 +19,18 @@ public final class ActorMeta extends org.slim3.datastore.ModelMeta key = new org.slim3.datastore.CoreAttributeMeta(this, "__key__", "key", com.google.appengine.api.datastore.Key.class); + /** */ + public final org.slim3.datastore.StringAttributeMeta keyword = new org.slim3.datastore.StringAttributeMeta(this, "keyword", "keyword"); + /** */ public final org.slim3.datastore.StringAttributeMeta name = new org.slim3.datastore.StringAttributeMeta(this, "name", "name"); /** */ public final org.slim3.datastore.StringAttributeMeta password = new org.slim3.datastore.StringAttributeMeta(this, "password", "password"); + /** */ + public final org.slim3.datastore.CoreAttributeMeta purchase = new org.slim3.datastore.CoreAttributeMeta(this, "purchase", "purchase", java.lang.Integer.class); + /** */ public final org.slim3.datastore.CoreAttributeMeta version = new org.slim3.datastore.CoreAttributeMeta(this, "version", "version", java.lang.Long.class); @@ -54,8 +60,10 @@ public jp.dogrun.ileaflet.model.Actor entityToModel(com.google.appengine.api.dat model.setEmail((java.lang.String) entity.getProperty("email")); model.setIdentity((java.lang.String) entity.getProperty("identity")); model.setKey(entity.getKey()); + model.setKeyword((java.lang.String) entity.getProperty("keyword")); model.setName((java.lang.String) entity.getProperty("name")); model.setPassword((java.lang.String) entity.getProperty("password")); + model.setPurchase(longToInteger((java.lang.Long) entity.getProperty("purchase"))); model.setVersion((java.lang.Long) entity.getProperty("version")); return model; } @@ -73,8 +81,10 @@ public com.google.appengine.api.datastore.Entity modelToEntity(java.lang.Object entity.setProperty("editAt", m.getEditAt()); entity.setProperty("email", m.getEmail()); entity.setProperty("identity", m.getIdentity()); + entity.setProperty("keyword", m.getKeyword()); entity.setProperty("name", m.getName()); entity.setProperty("password", m.getPassword()); + entity.setProperty("purchase", m.getPurchase()); entity.setProperty("version", m.getVersion()); entity.setProperty("slim3.schemaVersion", 1); return entity; @@ -161,6 +171,10 @@ protected void modelToJson(org.slim3.datastore.json.JsonWriter writer, java.lang writer.setNextPropertyName("key"); encoder0.encode(writer, m.getKey()); } + if(m.getKeyword() != null){ + writer.setNextPropertyName("keyword"); + encoder0.encode(writer, m.getKeyword()); + } if(m.getName() != null){ writer.setNextPropertyName("name"); encoder0.encode(writer, m.getName()); @@ -169,6 +183,10 @@ protected void modelToJson(org.slim3.datastore.json.JsonWriter writer, java.lang writer.setNextPropertyName("password"); encoder0.encode(writer, m.getPassword()); } + if(m.getPurchase() != null){ + writer.setNextPropertyName("purchase"); + encoder0.encode(writer, m.getPurchase()); + } if(m.getVersion() != null){ writer.setNextPropertyName("version"); encoder0.encode(writer, m.getVersion()); @@ -191,10 +209,14 @@ protected jp.dogrun.ileaflet.model.Actor jsonToModel(org.slim3.datastore.json.Js m.setIdentity(decoder0.decode(reader, m.getIdentity())); reader = rootReader.newObjectReader("key"); m.setKey(decoder0.decode(reader, m.getKey())); + reader = rootReader.newObjectReader("keyword"); + m.setKeyword(decoder0.decode(reader, m.getKeyword())); reader = rootReader.newObjectReader("name"); m.setName(decoder0.decode(reader, m.getName())); reader = rootReader.newObjectReader("password"); m.setPassword(decoder0.decode(reader, m.getPassword())); + reader = rootReader.newObjectReader("purchase"); + m.setPurchase(decoder0.decode(reader, m.getPurchase())); reader = rootReader.newObjectReader("version"); m.setVersion(decoder0.decode(reader, m.getVersion())); return m; diff --git a/src/jp/dogrun/ileaflet/model/Actor.java b/src/jp/dogrun/ileaflet/model/Actor.java index 77ce1f9..0a16fef 100644 --- a/src/jp/dogrun/ileaflet/model/Actor.java +++ b/src/jp/dogrun/ileaflet/model/Actor.java @@ -20,12 +20,14 @@ public class Actor implements Serializable { @Attribute(version = true) private Long version; - + private String identity; private String email; private String name; private String password; - + private Integer purchase; + private String keyword; + @Attribute(listener = CreationDate.class) private Date createAt; @Attribute(listener = ModificationDate.class) @@ -146,4 +148,20 @@ public String getPassword() { public void setPassword(String password) { this.password = password; } + + public Integer getPurchase() { + return purchase; + } + + public void setPurchase(Integer purchase) { + this.purchase = purchase; + } + + public String getKeyword() { + return keyword; + } + + public void setKeyword(String keyword) { + this.keyword = keyword; + } } diff --git a/src/jp/dogrun/ileaflet/util/ApplicationUtil.java b/src/jp/dogrun/ileaflet/util/ApplicationUtil.java index 241b49c..c914a46 100644 --- a/src/jp/dogrun/ileaflet/util/ApplicationUtil.java +++ b/src/jp/dogrun/ileaflet/util/ApplicationUtil.java @@ -3,11 +3,22 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import javax.servlet.http.HttpServletRequest; + public class ApplicationUtil { + + public static String getHost(HttpServletRequest request) { + String url = request.getRequestURL().toString(); + String uri = request.getRequestURI(); + int idx = url.indexOf(uri); + String server = url.substring(0,idx); + return server; + } public static String changeMD5(String src) { return digest("MD5",src); } + private static String digest(String digest,String str) { if (str == null || str.length() == 0) { throw new IllegalArgumentException("文字列がNull、または空です。"); diff --git a/test/jp/dogrun/ileaflet/controller/login/RegistrationControllerTest.java b/test/jp/dogrun/ileaflet/controller/login/RegistrationControllerTest.java new file mode 100644 index 0000000..c536e30 --- /dev/null +++ b/test/jp/dogrun/ileaflet/controller/login/RegistrationControllerTest.java @@ -0,0 +1,18 @@ +package jp.dogrun.ileaflet.controller.login; + +import org.slim3.tester.ControllerTestCase; +import org.junit.Test; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; + +public class RegistrationControllerTest extends ControllerTestCase { + + @Test + public void run() throws Exception { + tester.start("/login/registration"); + RegistrationController controller = tester.getController(); + assertThat(controller, is(notNullValue())); + assertThat(tester.isRedirect(), is(false)); + assertThat(tester.getDestinationPath(), is(nullValue())); + } +} diff --git a/war/WEB-INF/appengine-generated/datastore-indexes-auto.xml b/war/WEB-INF/appengine-generated/datastore-indexes-auto.xml index f57b476..d805369 100644 --- a/war/WEB-INF/appengine-generated/datastore-indexes-auto.xml +++ b/war/WEB-INF/appengine-generated/datastore-indexes-auto.xml @@ -1,4 +1,4 @@ - + diff --git a/war/WEB-INF/appengine-generated/local_db.bin b/war/WEB-INF/appengine-generated/local_db.bin index d69d761..8413f1c 100644 Binary files a/war/WEB-INF/appengine-generated/local_db.bin and b/war/WEB-INF/appengine-generated/local_db.bin differ diff --git a/war/dashboard/index.jsp b/war/dashboard/index.jsp index 6911078..2eaa337 100644 --- a/war/dashboard/index.jsp +++ b/war/dashboard/index.jsp @@ -1,4 +1,4 @@ -<%@page pageEncoding="UTF-8" isELIgnored="false" session="false"%> +<%@page pageEncoding="UTF-8" isELIgnored="false" session="true"%> diff --git a/war/login/index.jsp b/war/login/index.jsp index 6ec40d8..82b9ca6 100644 --- a/war/login/index.jsp +++ b/war/login/index.jsp @@ -9,8 +9,8 @@
- id: - pass: + id: + pass:
diff --git a/war/mainFrame.jsp b/war/mainFrame.jsp index a8572f7..1f12639 100644 --- a/war/mainFrame.jsp +++ b/war/mainFrame.jsp @@ -1,4 +1,4 @@ -<%@page pageEncoding="UTF-8" isELIgnored="false" session="false"%> +<%@page pageEncoding="UTF-8" isELIgnored="false" session="true"%>