X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=profile.js;h=9734638b60c2c17899442edf4b68007eead88f37;hb=be62e239c93c4d89b35b2b794ae2772116089845;hp=9b0a61c84063b4231d7422dab4e10bec0360195f;hpb=5eeb421df81cfb81161366c1f995a9cf7fe6f4f3;p=webchat%2FWebChat.git diff --git a/profile.js b/profile.js index 9b0a61c..9734638 100644 --- a/profile.js +++ b/profile.js @@ -194,17 +194,15 @@ function edit_postproc(req, res) return; }else if(typeof(req.body.edit) != "undefined"){ var validator = new Validator(); - if(validator.Validate(req.body,config.alias)) + var result = validator.Validate(req.body,config.alias,true); + if(result.length > 0) { - RenderMessage(validator.Message,req.session.items); + RenderMessage(res,result,req.session.items); return; } async.waterfall([ function(cb){ - if(typeof(req.body.updatepassword) == "undefined") - collection.UpdatAsync(req.body.name,req.body,null,cb); - else - collection.UpdatAsync(req.body.name,req.body,req.body.password,cb); + collection.UpdatAsync(req.body.name,req.body,cb); }, ],function(err,result){ if(err != null) @@ -221,9 +219,10 @@ function registor_postproc(req, res) { if(typeof(req.body.registor) != "undefined"){ var validator = new Validator(); - if(validator.Validate(req.body,config.alias)) + var result = validator.Validate(req.body,config.alias,false); + if(result.length > 0) { - RenderMessage(res,validator.Message,req.session.items); + RenderMessage(res,result,req.session.items); return; } async.waterfall([ @@ -252,6 +251,8 @@ function registor_proc(req, res) function RenderMessage(res,msg,info) { + if(typeof(msg) == "string") + msg = new Array(msg); if(typeof(info) == "undefined" || typeof(info.admin) == "undefined") res.render("profile/message",{message:msg,admin:false}); else @@ -263,22 +264,15 @@ function RenderMessage(res,msg,info) // function ProfileCollection() { - var MySQLPool = new require("./mysql_pool.js"); var murmurhash = require("murmurhash"); - var pool = new MySQLPool({ - host : config.db_host, - user : config.db_user, - password : config.db_password, - port : config.db_port, - database : "webchat", - }); + var Profile = require("./init").GetProfileColletion; this.AuthAsync = function(name,password,cb){ async.waterfall([ function(next){ - pool.query("SELECT password FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],next); + Profile.find({name_hash:murmurhash.v3(name), name:name}).done(next); }, function(result,next){ - if(result[0].password == md5_hex(password)) + if(result.password == md5_hex(password)) next(null,true); else next(null,false); @@ -286,32 +280,38 @@ function ProfileCollection() ],cb); } this.GetAsync = function(name,cb){ - pool.query("SELECT * FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],cb); + Profile.find({name_hash:murmurhash.v3(name), name:name}).done(cb); } this.AddAsync = function(data,cb){ - var item = GetItem(data);; - pool.query("INSERT INTO profilelist SET ?",[item],cb); + newProfile = Profile.build(GetItem(data)); + newProfile.save().done(cb); } - this.UpdatAsync = function(name,data,newpassword,cb){ - var item = GetItem(data); - if(newpassword != null) - item.password = md5_hex(newpassword); - pool.query("UPDATE profilelist SET ? WHERE name = ?",[item,name],cb); + this.UpdatAsync = function(name,data,cb){ + Profile.update(GetItem(data),{name_hash:murmurhash.v3(name), name:name}).done(cb); } this.ClearAsync = function(cb){ - pool.query("TRUNCATE TABLE profilelist",null,cb); + Profile.drop().done(cb); } this.RemoveRangeAsync = function(names,cb){ - pool.query("DELETE FROM profilelist WHERE name IN (?)",[names],cb); + Profile.destroy({where:{ + in:[names] + } + }).done(cb); } this.RemoveAsync = function(name,cb){ - pool.query("DELETE FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],cb); + Profile.destroy({name_hash:murmurhash.v3(name), name:name}).done(cb); } this.FindByNameAsync = function(pattern,start,count,cb){ - pool.query("SELECT * FROM profilelist WHERE name LIKE ? LIMIT ?,?",[pattern+"%",start,count],cb); + Profile.findAll({ + offset:start, + limit:count, + where:{ + like:pattern + } + }).done(cb); } this.ToArrayAsync = function(start,count,cb){ - pool.query("SELECT name,lastmodified FROM profilelist LIMIT ?,?",[start,count],cb); + Profile.findAll({offset:start,limit:count}).done(cb); } var crypto = require("crypto"); @@ -322,7 +322,7 @@ function ProfileCollection() return md5.digest('hex'); } - function GetItem(data) + function GetItem(data,newpw) { var item = { name_hash:murmurhash.v3(data.name), @@ -330,16 +330,24 @@ function ProfileCollection() }; for(var key in config.alias) { - if(key == "password") - { - item[key] = md5_hex(data[key]); - } - else if(config.alias[key].visible_edit) + if(typeof(config.alias[key].nodefinetable) != "undefined" && + config.alias[key].nodefinetable) + continue; + if(config.alias[key].visible_edit) { - if(data[key] == "") + if(config.alias[key].type == "password") + { + if(data[key] != "") + item[key] = md5_hex(data[key]); + } + else if(data[key] == "" && typeof(config.alias[key].defaultvalue) != "undefined") + { item[key] = config.alias[key].defaultvalue; + } else + { item[key] = data[key]; + } } } return item; @@ -357,9 +365,9 @@ function Validator() // // @body バリテーションの対象となる連想配列 // @alias バリテーションを行う要素のリスト - this.Validate = function(body,alias){ - var result = false; - this.Message = ""; + // @editflag 編集時なら真 + this.Validate = function(body,alias,editflag){ + var result = new Array(); for(var key in alias) { if(alias[key].visible_edit == false) @@ -368,17 +376,20 @@ function Validator() if(typeof(alias[key].isnotempty) != "undefined" && alias[key].isnotempty && body[key] == "") message = resource.is_not_empty; - message = IsValidate(body[key],alias[key].type,alias[key].rule); + else if(typeof(alias[key].isnotemptyonregistor) != "undefined" && + alias[key].isnotemptyonregistor && body[key] == "" && + !editflag) + message = resource.is_not_empty; + else if(typeof(alias[key].mustmatchitem) != "undefined" && + body[key] != body[alias[key].mustmatchitem]) + message = util.format(resource.must_match_item,alias[alias[key].mustmatchitem].name); + else + message = IsValidate(body[key],alias[key].type,alias[key].rule); if(message != null) - { - this.Message += "

" + alias[key].name + ":" + message + "

\n"; - result = true; - } + result.push(alias[key].name + ":" + message); } return result; } - // バリテーション時にエラーがあった場合、メッセージが記録される - this.Message = ""; function IsValidate(data,type,rule){ if(typeof(data) == "undefined") throw "data is undefined";