Improving users management

--HG--
branch : dev
extra : transplant_source : %AA%18%27%A6%83%7B%E7o%24%CF%11%81%D7%84%9D%0C%FB%D11%FB
This commit is contained in:
Sebastien Ros
2011-01-15 09:33:29 -08:00
parent 9adfa13a56
commit b1c782004b
5 changed files with 184 additions and 210 deletions

View File

@@ -1,6 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Core.Contents.Controllers;
using Orchard.Core.Settings.Models;
using Orchard.DisplayManagement;
using Orchard.Localization;
@@ -12,6 +15,7 @@ using Orchard.Users.ViewModels;
using Orchard.Mvc.Extensions;
using System;
using Orchard.Settings;
using Orchard.UI.Navigation;
namespace Orchard.Users.Controllers {
[ValidateInput(false)]
@@ -39,24 +43,99 @@ namespace Orchard.Users.Controllers {
public IOrchardServices Services { get; set; }
public Localizer T { get; set; }
public ActionResult Index() {
public ActionResult Index(UserIndexOptions options, PagerParameters pagerParameters) {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to list users")))
return new HttpUnauthorizedResult();
var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
// default options
if (options == null)
options = new UserIndexOptions();
var users = Services.ContentManager
.Query<UserPart, UserPartRecord>()
.Where(x => x.UserName != null)
.List();
.Query<UserPart, UserPartRecord>();
switch (options.Filter) {
case UsersFilter.Approved:
users = users.Where(u => u.RegistrationStatus == UserStatus.Approved);
break;
case UsersFilter.Pending:
users = users.Where(u => u.RegistrationStatus == UserStatus.Pending);
break;
case UsersFilter.EmailPending:
users = users.Where(u => u.EmailStatus == UserStatus.Approved);
break;
}
if(!String.IsNullOrWhiteSpace(options.Search)) {
users = users.Where(u => u.UserName.Contains(options.Search) || u.Email.Contains(options.Search));
}
var pagerShape = Shape.Pager(pager).TotalItemCount(users.Count());
switch (options.Order) {
case UsersOrder.Name:
users = users.OrderBy(u => u.UserName);
break;
case UsersOrder.Email:
users = users.OrderBy(u => u.Email);
break;
}
var results = users
.Slice(pager.GetStartIndex(), pager.PageSize)
.ToList();
var model = new UsersIndexViewModel {
Rows = users
.Select(x => new UsersIndexViewModel.Row { UserPart = x })
.ToList()
Users = results
.Select(x => new UserEntry { User = x.Record })
.ToList(),
Options = options,
Pager = pagerShape
};
return View(model);
}
[HttpPost]
[FormValueRequired("submit.BulkEdit")]
public ActionResult Index(FormCollection input) {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage users")))
return new HttpUnauthorizedResult();
var viewModel = new UsersIndexViewModel {Users = new List<UserEntry>(), Options = new UserIndexOptions()};
UpdateModel(viewModel);
var checkedEntries = viewModel.Users.Where(c => c.IsChecked);
switch (viewModel.Options.BulkAction) {
case UsersBulkAction.None:
break;
case UsersBulkAction.Approve:
foreach (var entry in checkedEntries) {
Approve(entry.User.Id);
}
break;
case UsersBulkAction.Disable:
foreach (var entry in checkedEntries) {
Moderate(entry.User.Id);
}
break;
case UsersBulkAction.ChallengeEmail:
foreach (var entry in checkedEntries) {
SendChallengeEmail(entry.User.Id);
}
break;
case UsersBulkAction.Delete:
foreach (var entry in checkedEntries) {
Delete(entry.User.Id);
}
break;
}
return RedirectToAction("Index");
}
public ActionResult Create() {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage users")))
return new HttpUnauthorizedResult();
@@ -109,7 +188,7 @@ namespace Orchard.Users.Controllers {
}
Services.Notifier.Information(T("User created"));
return RedirectToAction("edit", new { user.Id });
return RedirectToAction("Index");
}
public ActionResult Edit(int id) {
@@ -163,7 +242,7 @@ namespace Orchard.Users.Controllers {
}
Services.Notifier.Information(T("User information updated"));
return RedirectToAction("Edit", new { id });
return RedirectToAction("Index");
}
public ActionResult Delete(int id) {
@@ -181,7 +260,7 @@ namespace Orchard.Users.Controllers {
}
else{
Services.ContentManager.Remove(user.ContentItem);
Services.Notifier.Information(T("User deleted"));
Services.Notifier.Information(T("User {0} deleted", user.UserName));
}
}
@@ -192,13 +271,13 @@ namespace Orchard.Users.Controllers {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage users")))
return new HttpUnauthorizedResult();
var user = Services.ContentManager.Get(id);
var user = Services.ContentManager.Get<IUser>(id);
if ( user != null ) {
_userService.SendChallengeEmail(user.As<UserPart>(), nonce => Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new {Area = "Orchard.Users", nonce = nonce})));
Services.Notifier.Information(T("Challenge email sent to {0}", user.UserName));
}
Services.Notifier.Information(T("Challenge email sent"));
return RedirectToAction("Index");
}
@@ -207,11 +286,11 @@ namespace Orchard.Users.Controllers {
if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage users")))
return new HttpUnauthorizedResult();
var user = Services.ContentManager.Get(id);
var user = Services.ContentManager.Get<IUser>(id);
if ( user != null ) {
user.As<UserPart>().RegistrationStatus = UserStatus.Approved;
Services.Notifier.Information(T("User approved"));
Services.Notifier.Information(T("User {0} approved", user.UserName));
}
return RedirectToAction("Index");