Skip to content

Commit

Permalink
登録ロジック(+メール送信)の追加
Browse files Browse the repository at this point in the history
  • Loading branch information
secondarykey committed Jan 18, 2013
1 parent 5ff7a83 commit 437e492
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 20 deletions.
33 changes: 29 additions & 4 deletions src/jp/dogrun/ileaflet/controller/login/LogonController.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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/");
}
}
74 changes: 66 additions & 8 deletions src/jp/dogrun/ileaflet/controller/login/RegisterController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
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;
import jp.dogrun.ileaflet.util.ApplicationUtil;

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 {

Expand All @@ -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");
Expand All @@ -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("[email protected]", "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);
}

}
Original file line number Diff line number Diff line change
@@ -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/");
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ public SamePasswordValidator samePassword() {
public SameActorValidator sameUser() {
return SameActorValidator.INSTANCE;
}

public KeywordValidator keyword() {
return KeywordValidator.INSTANCE;
}

}
8 changes: 8 additions & 0 deletions src/jp/dogrun/ileaflet/dao/ActorDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}


}
24 changes: 23 additions & 1 deletion src/jp/dogrun/ileaflet/meta/ActorMeta.java
Original file line number Diff line number Diff line change
@@ -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<jp.dogrun.ileaflet.model.Actor> {

Expand All @@ -19,12 +19,18 @@ public final class ActorMeta extends org.slim3.datastore.ModelMeta<jp.dogrun.ile
/** */
public final org.slim3.datastore.CoreAttributeMeta<jp.dogrun.ileaflet.model.Actor, com.google.appengine.api.datastore.Key> key = new org.slim3.datastore.CoreAttributeMeta<jp.dogrun.ileaflet.model.Actor, com.google.appengine.api.datastore.Key>(this, "__key__", "key", com.google.appengine.api.datastore.Key.class);

/** */
public final org.slim3.datastore.StringAttributeMeta<jp.dogrun.ileaflet.model.Actor> keyword = new org.slim3.datastore.StringAttributeMeta<jp.dogrun.ileaflet.model.Actor>(this, "keyword", "keyword");

/** */
public final org.slim3.datastore.StringAttributeMeta<jp.dogrun.ileaflet.model.Actor> name = new org.slim3.datastore.StringAttributeMeta<jp.dogrun.ileaflet.model.Actor>(this, "name", "name");

/** */
public final org.slim3.datastore.StringAttributeMeta<jp.dogrun.ileaflet.model.Actor> password = new org.slim3.datastore.StringAttributeMeta<jp.dogrun.ileaflet.model.Actor>(this, "password", "password");

/** */
public final org.slim3.datastore.CoreAttributeMeta<jp.dogrun.ileaflet.model.Actor, java.lang.Integer> purchase = new org.slim3.datastore.CoreAttributeMeta<jp.dogrun.ileaflet.model.Actor, java.lang.Integer>(this, "purchase", "purchase", java.lang.Integer.class);

/** */
public final org.slim3.datastore.CoreAttributeMeta<jp.dogrun.ileaflet.model.Actor, java.lang.Long> version = new org.slim3.datastore.CoreAttributeMeta<jp.dogrun.ileaflet.model.Actor, java.lang.Long>(this, "version", "version", java.lang.Long.class);

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions src/jp/dogrun/ileaflet/model/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 437e492

Please sign in to comment.