AppUser.cs
6.81 KB
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
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
namespace AIAHTML5.Server.Models
{
[BsonIgnoreExtraElements]
public class AppUser
{
public ObjectId _id { get; set; }
public string name;
public string emailAddress;
public string mobileNumber;
public string password;
internal static dynamic GetRegisteredCustomers()
{
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var usersCol = db.GetCollection<dynamic>("users");
var customers = usersCol.Find(new BsonDocument()).ToList();
return customers;
}
public string[] apps;
[BsonIgnore]
public bool successful;
[BsonIgnore]
public string message;
internal static bool IsResetPasswordLinkExpired(string userId)
{
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<BsonDocument>("users");
var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse(userId)) & Builders<BsonDocument>.Filter.Exists("resetPasswordLinkValidity");
var user = col.Find(filter).SingleOrDefault();
if (user == null)
return true;
else
{
DateTime expiryDateTime = user["resetPasswordLinkValidity"].ToUniversalTime();
DateTime currentDateTime = DateTime.UtcNow;
if (currentDateTime > expiryDateTime)
return true;
else
return false;
}
}
internal static void UpdatePassword(string userId, string password)
{
if (!IsResetPasswordLinkExpired(userId))
{
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<dynamic>("users");
var result = col.UpdateOne(Builders<dynamic>.Filter.Eq("_id", ObjectId.Parse(userId)),
Builders<dynamic>.Update
.Set("password", password)
.Set("lastPasswordChangeDate", DateTime.UtcNow)
.Unset("resetPasswordLinkValidity"));
}
}
public static dynamic Authenticate(string applicationName, string username, string password)
{
try
{
var emailFilter = Builders<dynamic>.Filter.Eq("emailAddress", username) & Builders<dynamic>.Filter.Eq("password", password);
var passwordFilter = Builders<dynamic>.Filter.Eq("mobileNumber", username) & Builders<dynamic>.Filter.Eq("password", password);
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<dynamic>("users");
dynamic user = col.Find(emailFilter).SingleOrDefault();
if (user == null)
user = col.Find(passwordFilter).SingleOrDefault();
user.successful = true;
return user;
}
catch (Exception ex)
{
return null;
}
}
public bool isRegistered()
{
return false;
}
public static bool isRegistered(string emailAddress, string mobileNumber)
{
FilterDefinition<AppUser>[] filters = { Builders<AppUser>.Filter.Eq("emailAddress", emailAddress), Builders<AppUser>.Filter.Eq("mobileNumber", mobileNumber) };
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<AppUser>("users");
List<AppUser> list;
list = col.Find(Builders<AppUser>.Filter.Or(filters)).ToList();
if (list.Count > 0)
return true;
else
return false;
}
public static bool isRegistered(string emailAddress)
{
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<AppUser>("users");
List<AppUser> list;
list = col.Find(Builders<AppUser>.Filter.Eq("emailAddress", emailAddress)).ToList();
if (list.Count > 0)
return true;
else
return false;
}
public static dynamic GetUser(string userId)
{
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<dynamic>("users");
dynamic userDetails = col.Find(Builders<dynamic>.Filter.Eq("_id", ObjectId.Parse(userId))).SingleOrDefault();
return userDetails;
}
public static AppUser Create(JObject userObject)
{
AppUser user = userObject.ToObject<AppUser>();
if (isRegistered(user.emailAddress, user.mobileNumber))
{
throw new Exception("Our records indicate that you are already registered.");
}
else
{
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<AppUser>("users");
user.name = user.name.ToTitleCase();
col.InsertOne(user);
user.successful = true;
user.message = "User created successfully.";
return user;
}
}
public static string SetPasswordResetExpiry(string username)
{
if (!isRegistered(username))
{
throw new Exception("This email address is not registered with us.");
}
var client = new MongoClient();
var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]);
var col = db.GetCollection<dynamic>("users");
var result = col.UpdateOne(Builders<dynamic>.Filter.Eq("emailAddress", username),
Builders<dynamic>.Update.Set("resetPasswordLinkValidity", DateTime.UtcNow.AddDays(1).ToUniversalTime()));
var user = col.Find(Builders<dynamic>.Filter.Eq("emailAddress", username)).SingleOrDefault();
return user._id.ToString();
}
}
}