This repository was archived by the owner on Dec 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateUser.cshtml
More file actions
154 lines (126 loc) · 5.84 KB
/
UpdateUser.cshtml
File metadata and controls
154 lines (126 loc) · 5.84 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
@{
/*
TODO: This needs to be updated. Not done.
It was copied from EditProduct but not updated to apply to
editing a User
*/
Layout = "~/_SiteLayout.cshtml";
Page.Title = "User Administration";
// deny access to any user not an Admin
if (!User.IsInRole("Admin")) {
Response.Redirect("Account/Login");
}
//open the db
var db =Database.Open("StarterSite");
// get the userID to edit
var editUserID = UrlData[0];
// if there was no userID to edit, redirect
if(editUserID.IsEmpty()){
Response.Redirect(@Href("~/Admin"));
}
// Setup validation
Validation.RequireField("username", "You must specify a username.");
Validation.RequireField("email", "You must specify an email address.");
//Username Validation
Validation.Add("username",
Validator.StringLength(
maxLength: Int32.MaxValue,
minLength: 5,
errorMessage: "username must be at least 5 characters"));
var selectQueryString = "SELECT * " +
"FROM UserProfile " +
"WHERE UserId=@0";
// query the db and get the variables of the user to edit
var row = db.QuerySingle(selectQueryString, editUserID);
var email = row.Email;
var username = row.Username;
var picture = row.Picture;
// setup variables for updating image
WebImage photo = null;
var newFileName = "";
var imagePath = "";
// Update the User when the button is clicked
if(IsPost){
// get the form entries
username = Request["username"];
email = Request["email"];
picture = WebImage.GetImageFromRequest();
// validate fields
if(String.IsNullOrEmpty(username)){
ModelState.AddError("username","Username is required");
}
if(String.IsNullOrEmpty(email)){
ModelState.AddError("email","Email address is required");
}
// Commented out because we allow a user to use the default picture.
//if(String.IsNullOrEmpty(picture)){
// ModelState.AddError("picture","A jpg or png picture is required");
//}
if(ModelState.IsValid){
// Check if email already exists
var matchingEmail = db.QuerySingle("SELECT Email FROM UserProfile " +
"WHERE LOWER(Email) = LOWER(@0) " +
"AND UserId != @1", email, editUserID);
//Check if Username already exists
var matchingUsername = db.QuerySingle("SELECT Username FROM UserProfile " +
"WHERE LOWER(Username) = LOWER(@0) " +
"AND UserId != @1", username, editUserID);
//EMAIL AND USERNAME AND IMAGE
if (matchingEmail == null && matchingUsername == null) {
//UPLOAD IMAGE:
if(picture != null){
// TODO: Better filename choice??
newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(picture.FileName);
imagePath = @"Images\" + newFileName;
picture.Save(@"~\" + imagePath);
}
// TODO: Default image needs to be set if photo==null
var updateQuery="UPDATE UserProfile " +
"SET Username=@0, Email=@1, Picture=@2 " +
"WHERE UserId=@3";
db.Execute(updateQuery, username, email, newFileName, editUserID);
Response.Redirect(@Href("~/Admin"));
} else {
// User already exists in db
ModelState.AddFormError("Email or username address is already in use.");
} //end else
} //end Validation(IsValid)
} // END If(IsPost)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Update Product</title>
</head>
<body>
<h1>Update Product</h1>
@Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.", excludeFieldErrors: true, htmlAttributes: null)
<form method="post" action="" enctype="multipart/form-data">
<fieldset>
<legend>Update User</legend> <!-- TODO: CSS Style this field-->
<ol>
<li>
<label for="username" @if(!ModelState.IsValidField("username")){<text>class="error-label"</text>}>Username</label>
<input type="text" id="username" name="username" value="@username" @Validation.For("username") autofocus="true" />
@* Write any email validation errors to the page *@
@Html.ValidationMessage("username")
</li>
<li>
<label for="email" @if(!ModelState.IsValidField("email")){<text>class="error-label"</text>}>Email address</label>
<input type="email" id="email" name="email" value="@email" @Validation.For("email") />
@* Write any email validation errors to the page *@
@Html.ValidationMessage("email")
</li>
<li class="image-upload"> <!--TODO: CSS Style: class and id-->
<label for="Image">Image</label>
<input type="file" id="Image" name="Image" /> <br/>
@* TODO: Validation like previous entries? Also value="" field *@
@Html.ValidationMessage("Image")
</li>
</ol>
<input type="submit" value="Update User" name="submit">
</fieldset>
</form>
</body>
</html>