forked from bioversity/Crop-Ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usermodel.js
107 lines (93 loc) · 3.23 KB
/
usermodel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
importPackage(java.lang);
var usermodel = {
emailExists: function(email) {
var result = googlestore.query("user")
.filter("email", "=", email)
.fetch(1);
if(result.length)
return true;
else
return false;
},
usernameExists: function(username) {
var result = googlestore.query("user")
.filter("username", "=", username)
.fetch(1);
if(result.length)
return true;
else
return false;
},
validUsername: function(username) {
var re = /^(?=.{4})(?!.{21})[\w.-]*[a-z][\w-.]*$/i;
return username.match(re);
},
validateEmail: function(email){
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return email.match(re);
},
// makes a sha1 string out of a string
sha1: function(text) {
function bytesToHex(b) {
var hexDigit = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
var buf = new StringBuffer();
for (var j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}
var md = java.security.MessageDigest.getInstance("SHA-1");
md.update(text.getBytes("iso-8859-1"), 0, text.length());
var sha1hash = md.digest();
return bytesToHex(sha1hash);
},
hideEmail: function(email) {
return email.split("@")[0];
},
// makes a md5 string out of a string
md5: function(text) {
function bytesToHex(b) {
var hexDigit = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
var buf = new StringBuffer();
for (var j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}
var md = java.security.MessageDigest.getInstance("MD5");
md.update(text.getBytes("iso-8859-1"), 0, text.length());
var hash = md.digest();
return bytesToHex(hash).toLowerCase();
},
/**
* takse care of outputting the user entity as a JS object
* and making sure password and other fields are not show
*/
out: function(u) {
var props = [
"username",
"created",
"email"
];
var ret = {};
// do gravatar
ret["gravatar"] = ""+usermodel.md5(u.getProperty("email").trim().toLowerCase());
for(var i=0; i<props.length; i++) {
var value = u.getProperty(props[i]);
if(value instanceof Text)
value = value.getValue();
if(value == null) value = "";
ret[props[i]] = ""+value;
}
// hide email
ret["email"] = usermodel.hideEmail(ret["email"]);
// also we want the id
ret["userid"] = ""+u.getKey().getId();
return ret;
}
};
exports = usermodel;