Created
May 24, 2017 16:09
-
-
Save aki017/a35eedf4ada6867938fe5ea1f55fdfa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myFunction() { | |
var user_name = "aki"; | |
var pass = "1234567"; | |
var data = {data: "dummy"}; | |
var score = Math.random(); | |
main("Log", user_name, pass, data, score); | |
} | |
function hash(key, value) { | |
return Utilities.base64Encode(Utilities.computeHmacSha256Signature(value, key)); | |
} | |
function main(table, user_name, pass, data, score){ | |
pass = hash(user_name, pass); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Users"); | |
var uid = findOrCreate(user_name, pass); | |
if(uid == -1){ | |
return false; | |
} | |
addRecord("Log", uid, {h: 2}, 0.5); | |
} | |
function addRecord(table, uid, obj, score) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(table); | |
sheet.getRange(102, 1, 1, 3).setValues([[uid, obj, score]]); | |
sheet.sort(3); | |
} | |
function findOrCreate(user_name, pass) { | |
var uid = find(user_name, pass); | |
if(uid > 0){ | |
return uid; | |
} | |
if(uid != -1){ | |
return uid; | |
} | |
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Users").appendRow([user_name, pass, new Date(), new Date()]); | |
return find(user_name, pass); | |
} | |
function find(user_name, pass) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Users"); | |
var userCount = sheet.getLastRow(); | |
var data = sheet.getRange(1, 1, userCount, 2).getValues(); | |
for(var i = 0; i < userCount; i++) { | |
if(data[i]==null) continue; | |
if(data[i][0] == user_name) { | |
if(data[i][1] == pass) { | |
return i; | |
}else{ | |
// invalid password | |
return -2; | |
} | |
} | |
} | |
return -1; | |
} | |
function doGet(e) | |
{ | |
var user_name = e.parameter["name"]; | |
var pass = e.parameter["pass"]; | |
var data = e.parameter["data"]; | |
var score = e.parameter["score"]; | |
var table = e.parameter["table"]; | |
if(data == undefined){ | |
var result = main(table, user_name, pass, data, score); | |
} | |
var d = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(table).getRange(1, 1, 100, 3).getValues(); | |
return ContentService.createTextOutput(JSON.stringify(d)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment