My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
´╗┐using System;
using Castle.ActiveRecord;
using NHibernate.Expression;
using System.Web;
using System.Web.Caching;
using Utilities;
using System.Text;
using System.Security.Cryptography;

namespace Entities
{
[ActiveRecord(Table = "Operators")]
public class Operator : ActiveRecordBase<Operator>
{
private int _id;
private DateTime? _created;
private string _username;
private string _email;
private string _friendlyName;
private string _password;
private bool _isAdmin;

[PrimaryKey]
public int ID
{
get { return _id; }
set { _id = value; }
}

[Property]
public bool IsAdmin
{
get { return _isAdmin; }
set { _isAdmin = value; }
}

[Property]
public DateTime? Created
{
get { return _created; }
set { _created = value; }
}

[Property]
public string Username
{
get { return _username; }
set { _username = value; }
}

[Property]
public string Email
{
get { return _email; }
set { _email = value; }
}

[Property]
public string FriendlyName
{
get { return _friendlyName; }
set { _friendlyName = value; }
}

[Property]
public string Password
{
get { return _password; }
set { _password = value; }
}

public bool CanDeleteQuestion
{
get
{
return IsAdmin || CalculateCreds >= Settings.CredsNeededToDeleteQuestion;
}
}

public bool CanEditQuestion
{
get
{
return IsAdmin || CalculateCreds >= Settings.CredsNeededToEditQuestion;
}
}

public bool CanDeleteAnswer
{
get
{
return IsAdmin || CalculateCreds >= Settings.CredsNeededToDeleteAnswer;
}
}

public bool CanEditAnswer
{
get
{
return IsAdmin || CalculateCreds >= Settings.CredsNeededToEditAnswer;
}
}

public static Operator Current
{
get { return HttpContext.Current.Session["__CurrentOperator"] as Operator; }
}

public static void Logout()
{
HttpContext.Current.Session["__CurrentOperator"] = null;
HttpCookie c = new HttpCookie("username", "mumboJumbo|zxzxzx");
c.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(c);
}

public static void LoginOpenID(string username, string friendlyName, bool publicTerminal)
{
// Escaping username since often it contains things which we cannot legally use in Stacked like
// e.g. "http://" etc...
username = username.Replace("http", "").Replace("https", "");
int index = 0;
while (index < username.Length)
{
if (("abcdefghijklmnopqrstuvwxyz0123456789.-_").IndexOf(username[index]) == -1)
{
username = username.Substring(0, index) + username.Substring(index + 1);
}
else
index += 1;
}

Operator oper = Operator.FindOne(
Expression.Eq("Username", username));
if (oper == null)
{
oper = new Operator();
oper.Username = username;
oper.IsAdmin = false;
oper.FriendlyName = friendlyName;
oper.Password = Guid.NewGuid().ToString();
oper.Save();
}
StoreLoggedInOperatorToSessionAndCookie(!publicTerminal, oper);
}

public static bool Login(string username, string password, bool persist)
{
Operator oper = Operator.FindOne(
Expression.Eq("Username", username),
Expression.Eq("Password", password));
StoreLoggedInOperatorToSessionAndCookie(persist, oper);
return oper != null;
}

private static void StoreLoggedInOperatorToSessionAndCookie(bool persist, Operator oper)
{
HttpContext.Current.Session["__CurrentOperator"] = oper;

if (persist && oper != null)
{
// Creating persistant cookie to avoid having to log in again...
HttpCookie cookie = new HttpCookie("username", oper.Username + "|" + oper.Password.GetHashCode().ToString());
cookie.HttpOnly = true;
cookie.Expires = DateTime.Now.AddMonths(3);
HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{
// We must *destroy* the old cookie here...!
if (HttpContext.Current.Response.Cookies["username"] != null)
{
HttpCookie c = new HttpCookie("username", "mumboJumbo|zxzxzx");
c.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(c);
}
}
}

public static int GetCount()
{
return Count();
}

public override void Save()
{
this.FriendlyName = this.FriendlyName.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
this.Username = this.Username.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
if (_id == 0)
Created = DateTime.Now;
base.Save();
}

public static bool TryLoginFromCookie()
{
if (Current != null)
return true;
if (HttpContext.Current.Request.Cookies["username"] != null)
{
HttpCookie creds = HttpContext.Current.Request.Cookies["username"];
string username = creds.Value.Split('|')[0];
string hashedPwd = creds.Value.Split('|')[1];

// We want to *overwrite* to make sure only HTTP cookies are allowed here
if (!creds.HttpOnly)
{
// Overwriting existing cookie if it's not an HTTP-Only cookie...
HttpCookie n = new HttpCookie("username");
n.Value = username + "|" + hashedPwd;
HttpContext.Current.Response.Cookies.Add(n);
}

Operator oper = Operator.FindOne(
Expression.Eq("Username", username));
if (oper != null && oper.Password.GetHashCode().ToString() == hashedPwd)
{
HttpContext.Current.Session["__CurrentOperator"] = oper;
return true;
}
}
return false;
}

public string Gravatar
{
get
{
return string.Format("http://www.gravatar.com/avatar/{0}?s=32&d=identicon", MD5Hash(this.Email));
}
}

private string MD5Hash(string email)
{
if (string.IsNullOrEmpty(email))
return string.Empty;

StringBuilder emailHash = new StringBuilder();
MD5 md5 = MD5.Create();
byte[] emailBuffer = Encoding.ASCII.GetBytes(email);
byte[] hash = md5.ComputeHash(emailBuffer);

foreach (byte hashByte in hash)
emailHash.Append(hashByte.ToString("x2"));

return emailHash.ToString();
}

public int CalculateCreds
{
get { return GetCreds(); }
}

public int GetCreds()
{
// Checking cache first
if (HttpContext.Current.Cache["operatorCreds" + this.ID] != null)
return (int)HttpContext.Current.Cache["operatorCreds" + this.ID];

// Figuring out how many points the user have

// How many questions user have asked
int questionsAsked = QuizItem.Count(
Expression.Eq("CreatedBy", this),
Expression.IsNull("Parent"));

// How many answers/comments user have given
int answersComments = QuizItem.Count(
Expression.Eq("CreatedBy", this),
Expression.IsNotNull("Parent"));

// How many POINTS (plus votes) for the questions he has asked
int upVotesGivenForQuestions = 0;
QuizItem[] items = QuizItem.FindAll(Expression.Eq("CreatedBy", this), Expression.IsNull("Parent"));
if (items != null)
{
foreach (QuizItem idx in items)
{
upVotesGivenForQuestions += idx.Score;
}
}

// How many POINTS (plus votes) for the ANSWERS he has given
int upVotesGivenForAnswers = 0;
items = QuizItem.FindAll(Expression.Eq("CreatedBy", this), Expression.IsNotNull("Parent"));
if (items != null)
{
foreach (QuizItem idx in items)
{
upVotesGivenForAnswers += idx.Score;
}
}

// How many users have favorited his questions
int numberOfFavoritesForQuestions = 0;
items = QuizItem.FindAll(Expression.Eq("CreatedBy", this), Expression.IsNull("Parent"));
if (items != null)
{
foreach (QuizItem idx in items)
{
numberOfFavoritesForQuestions += idx.CountFavorites(this);
}
}


// Algo is basically
// 1 point answer and comment given
// 2 point per question asked
// Total score of all answers * 5 (remember that negative votes then counts *-5*)
// Total score of all questions * 10 (remember that negative votes then counts *-5*)
// Number of people favoriting his questions * 20

int creds = 0;
creds += answersComments;
creds += questionsAsked * 2;
creds += (upVotesGivenForAnswers * 5);
creds += (upVotesGivenForQuestions * 10);
creds += (numberOfFavoritesForQuestions * 20);

// Adding to cache
HttpContext.Current.Cache.Insert(
"operatorCreds" + this.ID,
creds,
null,
DateTime.Now.AddMinutes(5),
Cache.NoSlidingExpiration);
return creds;
}

public new static int Count()
{
return ActiveRecordBase<Operator>.Count();
}
}
}

Change log

r75 by polterguy on Jan 6, 2009   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r74 by kariem.elkoush on Dec 16, 2008   Diff
[No log message]
r71 by kariem.elkoush on Dec 11, 2008   Diff
Added Profile Email and Gravatar
Support
r64 by polterguy on Dec 8, 2008   Diff
[No log message]
All revisions of this file

File info

Size: 11259 bytes, 334 lines
Powered by Google Project Hosting