-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileScreen.cs
228 lines (204 loc) · 9.55 KB
/
ProfileScreen.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Data.SqlClient;
namespace prelab1
{
public partial class ProfileScreen : Form
{
// allows to retrieve data
public static ProfileScreen instance;
public ProfileScreen()
{
InitializeComponent();
instance = this;
}
//Hashed the password
public static string ComputeSha256Hash(string rawData)
{
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
// Update ( saves changes )
private void btnsave_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"../../Information.xml");
if (Login.instance.admin) // for admin
{
foreach (XmlNode node in doc.SelectNodes("//User"))
{
if (Manager.instance.user == node.SelectSingleNode("Username").InnerText)
{
node.SelectSingleNode("Username").InnerText = textusername.Text;
node.SelectSingleNode("Password").InnerText = ComputeSha256Hash(textpassword.Text);
node.SelectSingleNode("Name-Surname").InnerText = textnamesurname.Text;
node.SelectSingleNode("PhoneNumber").InnerText = textphonenumber.Text;
node.SelectSingleNode("Address").InnerText = textaddress.Text;
node.SelectSingleNode("City").InnerText = textcity.Text;
node.SelectSingleNode("Country").InnerText = textcountry.Text;
node.SelectSingleNode("E-mail").InnerText = textemail.Text;
node.SelectSingleNode("E-mail").InnerText = textemail.Text;
node.SelectSingleNode("BestScore").InnerText = txtboxbestscore.Text;
MessageBox.Show("User informations are updated!");
}
}
}
else // for user
{
Password form7 = new Password();
form7.ShowDialog();
// The password check is needed to update own information
if (Login.instance.password == Password.instance.passwordcontrol)
{
foreach (XmlNode node in doc.SelectNodes("//User"))
{
if (Login.instance.user == node.SelectSingleNode("Username").InnerText)
{
node.SelectSingleNode("Username").InnerText = textusername.Text;
node.SelectSingleNode("Password").InnerText = textpassword.Text;
node.SelectSingleNode("Name-Surname").InnerText = textnamesurname.Text;
node.SelectSingleNode("PhoneNumber").InnerText = textphonenumber.Text;
node.SelectSingleNode("Address").InnerText = textaddress.Text;
node.SelectSingleNode("City").InnerText = textcity.Text;
node.SelectSingleNode("Country").InnerText = textcountry.Text;
node.SelectSingleNode("E-mail").InnerText = textemail.Text;
node.SelectSingleNode("BestScore").InnerText = txtboxbestscore.Text;
MessageBox.Show("Your informations are updated!");
}
}
}
else
{
MessageBox.Show("You must enter your password for update!");
}
}
Close();
doc.Save(@"../../Information.xml");
}
// Upload the users information (Profile screen)
private void ProfileScreen_Load(object sender, EventArgs e)
{
ActiveControl = textpassword;
XmlDocument doc = new XmlDocument();
doc.Load(@"../../Information.xml");
if (Login.instance.admin) // for admin
{
foreach (XmlNode node in doc.SelectNodes("//User"))
{
if (Manager.instance.user == node.SelectSingleNode("Username").InnerText)
{
textusername.Text = node.SelectSingleNode("Username").InnerText;
textpassword.Text = node.SelectSingleNode("Password").InnerText;
textnamesurname.Text = node.SelectSingleNode("Name-Surname").InnerText;
textphonenumber.Text = node.SelectSingleNode("PhoneNumber").InnerText;
textaddress.Text = node.SelectSingleNode("Address").InnerText;
textcity.Text = node.SelectSingleNode("City").InnerText;
textcountry.Text = node.SelectSingleNode("Country").InnerText;
textemail.Text = node.SelectSingleNode("E-mail").InnerText;
txtboxbestscore.Text = node.SelectSingleNode("BestScore").InnerText;
}
}
}
else // for user
{
foreach (XmlNode node in doc.SelectNodes("//User"))
{
if (Login.instance.user == node.SelectSingleNode("Username").InnerText)
{
textusername.Text = node.SelectSingleNode("Username").InnerText;
textpassword.Text = node.SelectSingleNode("Password").InnerText;
textnamesurname.Text = node.SelectSingleNode("Name-Surname").InnerText;
textphonenumber.Text = node.SelectSingleNode("PhoneNumber").InnerText;
textaddress.Text = node.SelectSingleNode("Address").InnerText;
textcity.Text = node.SelectSingleNode("City").InnerText;
textcountry.Text = node.SelectSingleNode("Country").InnerText;
textemail.Text = node.SelectSingleNode("E-mail").InnerText;
txtboxbestscore.Text = node.SelectSingleNode("BestScore").InnerText;
}
}
}
}
public void SqlDelete()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=OOP;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Delete OOP where username=@username", con);
cmd.Parameters.AddWithValue("@username", textusername.Text);
cmd.ExecuteNonQuery();
con.Close();
}
// Deletes the user
private void btndelete_Click(object sender, EventArgs e)
{
SqlDelete();
XmlDocument doc = new XmlDocument();
doc.Load(@"../../Information.xml");
if (Login.instance.admin) // for admin
{
foreach (XmlNode node in doc.SelectNodes("//User"))
{
if (Manager.instance.user == node.SelectSingleNode("Username").InnerText)
{
node.ParentNode.RemoveChild(node);
doc.Save(@"../../Information.xml");
}
}
MessageBox.Show("User has been deleted!");
}
else // for user
{
Password form7 = new Password();
form7.ShowDialog();
// The password check is needed to update or delete own information
if (Login.instance.password == Password.instance.passwordcontrol)
{
foreach (XmlNode node in doc.SelectNodes("//User"))
{
if (Login.instance.user == node.SelectSingleNode("Username").InnerText)
{
node.ParentNode.RemoveChild(node);
MessageBox.Show("Your account has been deleted!");
doc.Save(@"../../Information.xml");
}
}
}
else
{
MessageBox.Show("You must enter your password for delete your profile!");
}
}
Close();
}
// cannot change username
private void textusername_TextChanged(object sender, EventArgs e)
{
textusername.ReadOnly = true;
}
private void textpassword_TextChanged(object sender, EventArgs e)
{
//textpassword.UseSystemPasswordChar = false;
//textpassword.PasswordChar = '\0';
}
}
}