-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.cs
188 lines (170 loc) · 6.95 KB
/
User.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for User
/// </summary>
public class User
{
string email;
string userID;
string lastName;
string firstName;
string password;
bool resetPassword;
List<UserRoles> userRoles = new List<UserRoles>();
public User(string email, string lastName, string firstName, string password)
{
this.email = email;
this.lastName = lastName;
this.firstName = firstName;
this.password = password;
}
public User(string email)
{
this.email = email;
}
public string Email
{
get { return email; }
set { email = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string Password
{
get { return password; }
set { password = value;
ResetPasswordForLogin(value); }
}
public string UserID
{
set { userID = value; }
}
public bool ResetPassword
{
get { return resetPassword; }
set { resetPassword = value;}
}
public void CreateUser()
{
string connection = ConfigurationManager.ConnectionStrings["EVENTSConnectionString"].ToString();
SqlConnection dbConnection = new SqlConnection(connection);
try
{
dbConnection.Open();
SqlParameter EmailParam = new SqlParameter("@EMAIL", System.Data.SqlDbType.NVarChar, 50);
EmailParam.Value = this.email;
SqlParameter FirstNameParam = new SqlParameter("@FIRST_NAME", System.Data.SqlDbType.NVarChar, 70);
FirstNameParam.Value = this.firstName;
SqlParameter LastNameParam = new SqlParameter("@LAST_NAME", System.Data.SqlDbType.NVarChar, 70);
LastNameParam.Value = this.lastName;
SqlParameter PasswordParam = new SqlParameter("@PASSWORD", System.Data.SqlDbType.NVarChar, 70);
PasswordParam.Value = Security.HashSHA1(this.password);
SqlParameter[] CreateUserParameters = new SqlParameter[] { EmailParam, LastNameParam, FirstNameParam, PasswordParam };
string createUserCommand = "INSERT INTO APPLICATION_USERS (EMAIL, FIRST_NAME, LAST_NAME, PASSWORD) VALUES (@EMAIL, @FIRST_NAME, @LAST_NAME, @PASSWORD);";
SqlCommand createUser = new SqlCommand(null, dbConnection);
createUser.Parameters.AddRange(CreateUserParameters);
createUser.CommandText = createUserCommand;
createUser.Prepare();
createUser.ExecuteNonQuery();
dbConnection.Close();
UserRoles createRole = new UserRoles(this);
}
catch (SqlException exception)
{
// "<p> Error Code " + exception.Number + ": " + exception.Message + "</p>";
}
}
public void GetUserInfo()
{
string connection = ConfigurationManager.ConnectionStrings["EVENTSConnectionString"].ToString();
SqlConnection dbConnection = new SqlConnection(connection);
try
{
dbConnection.Open();
SqlParameter EmailParam = new SqlParameter("@EMAIL", System.Data.SqlDbType.NVarChar, 50);
EmailParam.Value = this.email;
SqlParameter[] GetUserParameters = new SqlParameter[] { EmailParam };
string getUserCommand = "SELECT FIRST_NAME, LAST_NAME, RESET_PASSWORD FROM APPLICATION_USERS WHERE EMAIL = @EMAIL;";
SqlCommand getUser = new SqlCommand(null, dbConnection);
getUser.Parameters.AddRange(GetUserParameters);
getUser.CommandText = getUserCommand;
getUser.Prepare();
SqlDataReader getUserInfo = getUser.ExecuteReader();
if(getUserInfo.Read())
{
this.firstName = getUserInfo["FIRST_NAME"].ToString();
this.lastName = getUserInfo["LAST_NAME"].ToString();
this.resetPassword = getUserInfo["RESET_PASSWORD"].ToString() == "T";
}
dbConnection.Close();
}
catch (SqlException exception)
{
// "<p> Error Code " + exception.Number + ": " + exception.Message + "</p>";
}
}
public void ResetPasswordForLogin(string newPassword)
{
string connection = ConfigurationManager.ConnectionStrings["EVENTSConnectionString"].ToString();
SqlConnection dbConnection = new SqlConnection(connection);
try
{
dbConnection.Open();
SqlParameter EmailParam = new SqlParameter("@EMAIL", System.Data.SqlDbType.NVarChar, 50);
EmailParam.Value = this.email;
SqlParameter PasswordParam = new SqlParameter("@PASSWORD", System.Data.SqlDbType.NVarChar, 70);
PasswordParam.Value = Security.HashSHA1(newPassword);
SqlParameter[] ResetPasswordParameters = new SqlParameter[] { EmailParam, PasswordParam };
string resetPasswordCommand = "UPDATE APPLICATION_USERS SET PASSWORD = @PASSWORD WHERE EMAIL = @EMAIL;";
SqlCommand resetPassword = new SqlCommand(null, dbConnection);
resetPassword.Parameters.AddRange(ResetPasswordParameters);
resetPassword.CommandText = resetPasswordCommand;
resetPassword.Prepare();
resetPassword.ExecuteNonQuery();
dbConnection.Close();
}
catch (SqlException exception)
{
// "<p> Error Code " + exception.Number + ": " + exception.Message + "</p>";
}
}
public void ResetOnNextLogin(string reset)
{
string connection = ConfigurationManager.ConnectionStrings["EVENTSConnectionString"].ToString();
SqlConnection dbConnection = new SqlConnection(connection);
try
{
dbConnection.Open();
SqlParameter EmailParam = new SqlParameter("@EMAIL", System.Data.SqlDbType.NVarChar, 50);
EmailParam.Value = this.email;
SqlParameter ResetParam = new SqlParameter("@RESET", System.Data.SqlDbType.Char, 1);
ResetParam.Value = reset;
SqlParameter[] ResetNextLoginParameters = new SqlParameter[] { EmailParam, ResetParam };
string resetNextLoginCommand = "UPDATE APPLICATION_USERS SET RESET_PASSWORD = @RESET WHERE EMAIL = @EMAIL;";
SqlCommand resetNextLogin = new SqlCommand(null, dbConnection);
resetNextLogin.Parameters.AddRange(ResetNextLoginParameters);
resetNextLogin.CommandText = resetNextLoginCommand;
resetNextLogin.Prepare();
resetNextLogin.ExecuteNonQuery();
dbConnection.Close();
this.resetPassword = true;
}
catch (SqlException exception)
{
// "<p> Error Code " + exception.Number + ": " + exception.Message + "</p>";
}
}
}