forked from wojilu/wojilu
/
UserController.cs
executable file
·406 lines (313 loc) · 13.1 KB
/
UserController.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 2010, www.wojilu.com. All rights reserved.
*/
using System;
using System.Collections.Generic;
using wojilu.Net;
using wojilu.Web.Mvc.Attr;
using wojilu.Web.Utils;
using wojilu.Web.Mvc;
using wojilu.Common.Msg.Service;
using wojilu.Common.AppBase;
using wojilu.Common.Msg.Interface;
using wojilu.Members.Users.Service;
using wojilu.Members.Users.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Users.Interface;
using wojilu.Web.Controller.Users.Admin;
using wojilu.Web.Controller.Security;
using wojilu.Members.Interface;
using wojilu.Web.Mvc.Utils;
using wojilu.Web.Controller.Common;
using wojilu.Common.Resource;
namespace wojilu.Web.Controller.Admin.Members {
internal class UserVo {
public String Name { get; set; }
public String Pwd { get; set; }
public String Email { get; set; }
public String FriendlyUrl { get; set; }
}
public partial class UserController : ControllerBase {
private static readonly ILog logger = LogManager.GetLogger( typeof( UserController ) );
public IUserService userService { get; set; }
public ISiteRoleService roleService { get; set; }
public IMessageService msgService { get; set; }
public IAdminLogService<SiteLog> logService { get; set; }
public IConfirmEmail confirmEmail { get; set; }
public IUserErrorPicService errorPicService { get; set; }
public UserController() {
userService = new UserService();
roleService = new SiteRoleService();
msgService = new MessageService();
logService = new SiteLogService();
confirmEmail = new ConfirmEmail();
errorPicService = new UserErrorPicService();
}
public void Index() {
set( "userListLink", to( Index ) );
set( "SearchAction", to( Index ) );
set( "OperationUrl", to( Operation ) );
set( "sendMsgLink", to( SendMsg ) );
set( "sendEmailLink", to( SendEmail ) );
set( "sendConfirmEmailLink", to( SendConfirmMail ) );
set( "resetPwdLink", to( ResetPwd ) );
set( "errorPicLink", to( ApproveUserPic ) );
List<SiteRole> roles = roleService.GetAllRoles();
List<SiteRole> roles2 = roleService.GetRolesWithotGuest();
List<SiteRank> ranks = roleService.GetRankAll();
bindList( "roles", "role", roles );
bindList( "roles2", "r", roles2 );
bindList( "ranks", "rank", ranks );
String condition = getCondition();
DataPage<User> list = db.findPage<User>( condition );
bindUserList( list );
}
[HttpPost, DbTransaction]
public void Operation() {
String userIds = ctx.PostIdList( "choice" );
if (strUtil.IsNullOrEmpty( userIds )) {
redirect( Index );
return;
}
String condition = "Id in (" + userIds + ") ";
String cmd = ctx.Post( "action" );
String action = "";
if ("pick" == cmd) {
action = "set Status=" + MemberStatus.Pick;
}
else if ("unpick" == cmd) {
action = "set Status=" + MemberStatus.Normal;
}
else if ("approve" == cmd) {
action = "set Status=" + MemberStatus.Normal;
}
else if ("delete" == cmd) {
action = "set Status=" + MemberStatus.Deleted;
}
else if ("undelete" == cmd) {
action = "set Status=" + MemberStatus.Normal;
}
else if ("deletetrue" == cmd) {
User.deleteBatch( condition );
redirect( Index );
return;
}
else if ("category" == cmd) {
int roleId = ctx.PostInt( "categoryId" );
action = "set RoleId=" + roleId;
}
User.updateBatch( action, condition );
logUser( SiteLogString.AdminUser( cmd ), userIds );
content( "ok" );
}
//----------------------------------------------------------------------------
public void Edit( int id ) {
target( UpdateProfile, id );
User m = User.findById( id );
bindProfile( m );
set( "lnkEditName", to( EditName, id ) );
set( "lnkEditUrl", to( EditUrl, id ) );
}
[HttpPost, DbTransaction]
public void UpdateProfile( int id ) {
User m = User.findById( id );
UserProfileController.SaveProfile( m, ctx );
db.update( m );
db.update( m.Profile );
echoRedirectPart( lang( "opok" ) );
}
//--------------------------------------
public void EditName( int id ) {
target( UpdateName, id );
User m = User.findById( id );
set( "userName", m.Name );
}
[HttpPost,DbTransaction]
public void UpdateName( int id ) {
String newName = strUtil.SubString( ctx.Post( "userName" ), 20 );
if (strUtil.IsNullOrEmpty( newName )) {
echoError( "请填写用户名" );
return;
}
User m = User.findById( id );
m.Name = newName;
m.update();
echoToParentPart( lang( "opok" ) );
}
//--------------------------------------
public void EditUrl( int id ) {
target( UpdateUrl, id );
User m = User.findById( id );
set( "userUrl", m.Url );
}
[HttpPost, DbTransaction]
public void UpdateUrl( int id ) {
String newUrl = strUtil.SubString( ctx.Post( "userUrl" ), 50 );
if (strUtil.IsNullOrEmpty( newUrl )) {
echoError( "请填写个性网址" );
return;
}
User m = User.findById( id );
m.Url = newUrl;
m.update();
// UserApp
UserApp.updateBatch( "OwnerUrl='" + newUrl + "'", "OwnerId=" + id );
UserApp.updateBatch( "CreatorUrl='" + newUrl + "'", "OwnerId=" + id );
UserMenu.updateBatch( "OwnerUrl='" + newUrl + "'", "OwnerId=" + id );
echoToParentPart( lang( "opok" ) );
}
//----------------------------------------------------------------------------
public void ResetPwd() {
set( "ActionLink", to( SavePwd ) + "?id=" + ctx.GetIdList( "id" ) );
String idsStr = ctx.GetIdList( "id" );
List<User> users = userService.GetByIds( idsStr );
bindReceiverEmailList( users, idsStr );
}
[HttpPost, DbTransaction]
public void SavePwd() {
// 1、验证密码是否正确
String pwd = ctx.Post( "Pwd" );
// 2、修改密码
String idsStr = ctx.PostIdList( "UserIds" );
List<User> users = userService.GetByIds( idsStr );
if (users.Count == 0) {
echoRedirectPart( lang( "exUserNotFound" ) );
return;
}
foreach (User user in users) {
userService.UpdatePwd( user, pwd );
logUser( SiteLogString.UpdateUserPwd(), user );
}
// 3、发送到邮箱
Boolean isSendMail = (ctx.PostIsCheck( "chkSendEmail" ) == 1);
if (isSendMail) {
sendPwdToEmail( users, pwd );
}
else {
echoRedirectPart( lang( "pwdUpdated" ), to( Index ) );
}
}
private void sendPwdToEmail( List<User> users, String pwd ) {
MailClient mail = MailClient.Init();
String msgTitle = string.Format( lang( "newPwdInfo" ), config.Instance.Site.SiteName );
String msgBody = "{0} : <br/>" + string.Format( lang( "newPwdBody" ), config.Instance.Site.SiteName, pwd ) + config.Instance.Site.SiteName;
int sendCount = 0;
foreach (User user in users) {
if (isEmailValid( user ) == false) continue;
mail.Send( user.Email, msgTitle, string.Format( msgBody, user.Name ) );
sendCount++;
}
if (sendCount > 0)
echoRedirectPart( lang( "pwdUpdatedAndSent" ), to( Index ) );
else
echoRedirectPart( lang( "pwdUpdatedAndSentError" ), to( Index ) );
}
//----------------------------------------------------------------------------
public void SendMsg() {
set( "ActionLink", to( SaveMsg ) + "?id=" + ctx.GetIdList( "id" ) );
String idsStr = ctx.GetIdList( "id" );
List<User> users = userService.GetByIds( idsStr );
bindReceiverList( users, idsStr );
}
[HttpPost, DbTransaction]
public void SaveMsg() {
MsgInfo msgInfo = validateMsg( true );
if (ctx.HasErrors) {
run( SendMsg );
return;
}
msgService.SiteSend( msgInfo.Title, msgInfo.Body, msgInfo.Users );
echoRedirectPart( lang( "sentok" ), to( Index ) );
}
public void SendEmail() {
if (config.Instance.Site.EnableEmail == false) {
echo( "对不起,邮件服务尚未开启,无法发送邮件" );
return;
}
String ids = ctx.GetIdList( "id" );
set( "ActionLink", to( SaveEmail ) + "?id=" + ids );
List<User> users = userService.GetByIds( ids );
bindReceiverEmailList( users, ids );
}
public void SendConfirmMail() {
if (config.Instance.Site.EnableEmail == false) {
echo( "对不起,邮件服务尚未开启,无法发送邮件" );
return;
}
String ids = ctx.GetIdList( "id" );
set( "ActionLink", to( new EmailConfirmController().SendConfirmMail ) + "?id=" + ids );
List<User> users = userService.GetByIds( ids );
bindReceiverEmailList( users, ids );
set( "title", config.Instance.Site.SiteName + " " + lang( "accountConfirm" ) );
String emailBody = confirmEmail.GetEmailBody( users[0] );
set( "emailBody", emailBody );
String confirmTemplateTip = string.Format( lang( "confirmTemplateTip" ), to( new EmailConfirmController().EditTemplate ) );
set( "confirmTemplateTip", confirmTemplateTip );
}
[HttpPost, DbTransaction]
public void SaveEmail() {
if (config.Instance.Site.EnableEmail == false) {
echo( "对不起,邮件服务尚未开启,无法发送邮件" );
return;
}
MsgInfo msgInfo = validateMsg( true );
if (ctx.HasErrors) {
run( SendMsg );
return;
}
int sendCount = 0;
foreach (User user in msgInfo.Users) {
if (isEmailValid( user ) == false) continue;
Result sent = sendEmail( user, msgInfo.Title, msgInfo.Body );
if (sent.IsValid) {
logUser( SiteLogString.SendUserEmail(), user );
sendCount++;
}
}
if (sendCount > 0) {
echoRedirectPart( lang( "sentok" ), to( Index ) );
}
else {
echoRedirectPart( lang( "exSentError" ), to( Index ) );
}
}
private Result sendEmail( User user, string title, string msg ) {
return MailClient.Init().Send( user.Email, title, msg );
}
public void ApproveUserPic() {
set( "ActionLink", to( SaveApprovePic ) + "?id=" + ctx.GetIdList( "id" ) );
String idsStr = ctx.GetIdList( "id" );
List<User> users = userService.GetByIds( idsStr );
bindReceiverList( users, idsStr );
}
public void SaveApprovePic() {
String ids = ctx.PostIdList( "UserIds" );
String reviewMsg = validPicMsg();
if (ctx.HasErrors) {
run( ApproveUserPic );
return;
}
int isPass = ctx.PostInt( "IsPass" );
int isDelete = ctx.PostIsCheck( "IsDelete" );
if (isPass == 0) {
errorPicService.ApproveError( ids, reviewMsg, ctx.PostIsCheck( "IsNextAutoPass" ), isDelete );
}
else {
errorPicService.ApproveOk( ids, reviewMsg );
}
echoRedirectPart( "审核成功", to( Index ) );
}
private string validPicMsg() {
String idsStr = ctx.PostIdList( "UserIds" );
List<User> users = userService.GetByIds( idsStr );
if (users.Count == 0) {
errors.Add( lang( "exNoReceiver" ) );
return null;
}
String msg = strUtil.CutString( ctx.Post( "Msg" ), 200 );
if (strUtil.IsNullOrEmpty( msg )) errors.Add( "请填写审核原因" );
return msg;
}
}
}