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");

View File

@@ -4,10 +4,40 @@ using Orchard.Users.Models;
namespace Orchard.Users.ViewModels {
public class UsersIndexViewModel {
public class Row {
public UserPart UserPart { get; set; }
}
public IList<UserEntry> Users { get; set; }
public UserIndexOptions Options { get; set; }
public dynamic Pager { get; set; }
}
public IList<Row> Rows { get; set; }
public class UserEntry {
public UserPartRecord User { get; set; }
public bool IsChecked { get; set; }
}
public class UserIndexOptions {
public string Search { get; set; }
public UsersOrder Order { get; set; }
public UsersFilter Filter { get; set; }
public UsersBulkAction BulkAction { get; set; }
}
public enum UsersOrder {
Name,
Email
}
public enum UsersFilter {
All,
Approved,
Pending,
EmailPending
}
public enum UsersBulkAction {
None,
Delete,
Disable,
Approve,
ChallengeEmail
}
}

View File

@@ -1,52 +1,88 @@
@model Orchard.Users.ViewModels.UsersIndexViewModel
@using Orchard.Users.Models;
@using Orchard.Users.ViewModels;
@{
var userIndex = 0;
}
<h1>@Html.TitleForPage(T("Manage Users").ToString()) </h1>
@using (Html.BeginFormAntiForgeryPost()) {
@Html.ValidationSummary()
<div class="manage">@Html.ActionLink(T("Add a new user").ToString(), "Create", new { }, new { @class = "button primaryAction" })</div>
<fieldset>
<fieldset class="bulk-actions">
<label for="publishActions">@T("Actions:")</label>
<select id="publishActions" name="@Html.NameOf(m => m.Options.BulkAction)">
@Html.SelectOption(Model.Options.BulkAction, UsersBulkAction.None, T("Choose action...").ToString())
@Html.SelectOption(Model.Options.BulkAction, UsersBulkAction.Approve, T("Approve").ToString())
@Html.SelectOption(Model.Options.BulkAction, UsersBulkAction.Disable, T("Disable").ToString())
@Html.SelectOption(Model.Options.BulkAction, UsersBulkAction.ChallengeEmail, T("Send challenge E-mail").ToString())
@Html.SelectOption(Model.Options.BulkAction, UsersBulkAction.Delete, T("Remove").ToString())
</select>
<button type="submit" name="submit.BulkEdit" value="@T("Apply")">@T("Apply")</button>
</fieldset>
<fieldset class="bulk-actions">
@Html.TextBoxFor(m => m.Options.Search, new { @class = "text"})
<label for="filterResults">@T("Filter:")</label>
<select id="filterResults" name="@Html.NameOf(m => m.Options.Filter)">
@Html.SelectOption(Model.Options.Filter, UsersFilter.All, T("All Users").ToString())
@Html.SelectOption(Model.Options.Filter, UsersFilter.Approved, T("Approved Users").ToString())
@Html.SelectOption(Model.Options.Filter, UsersFilter.Pending, T("Pending Users").ToString())
@Html.SelectOption(Model.Options.Filter, UsersFilter.EmailPending, T("Pending Emails").ToString())
</select>
<label for="filterResults">@T("Sort by:")</label>
<select id="filterResults" name="@Html.NameOf(m => m.Options.Order)">
@Html.SelectOption(Model.Options.Order, UsersOrder.Name, T("Name").ToString())
@Html.SelectOption(Model.Options.Order, UsersOrder.Email, T("Email").ToString())
</select>
<button type="submit" name="submit.Filter" value="@T("Apply")">@T("Apply")</button>
</fieldset>
<fieldset>
<table class="items">
<colgroup>
<col id="Name" />
<col id="Email" />
<col id="Edit" />
</colgroup>
<thead>
<tr>
<th scope="col">&nbsp;&darr;</th>
<th scope="col">@T("Name")</th>
<th scope="col">@T("Email")</th>
<th scope="col">@T("") </th>
<th scope="col">@T("Actions")</th>
</tr>
</thead>
@foreach (var row in Model.Rows) {
<tr>
@foreach (var entry in Model.Users) {
<tr>
<td>
@if(row.UserPart.RegistrationStatus == UserStatus.Approved && row.UserPart.EmailStatus == UserStatus.Approved) {
<input type="hidden" value="@Model.Users[userIndex].User.Id" name="@Html.NameOf(m => m.Users[userIndex].User.Id)"/>
<input type="checkbox" value="true" name="@Html.NameOf(m => m.Users[userIndex].IsChecked)"/>
</td>
<td>
@if(entry.User.RegistrationStatus == UserStatus.Approved && entry.User.EmailStatus == UserStatus.Approved) {
<img class="icon" src="@Href("~/Modules/Orchard.Users/Content/Admin/images/online.gif") " alt="@T("Approved") " title="@T("User is approved") " />
}
else {
<img class="icon" src="@Href("~/Modules/Orchard.Users/Content/Admin/images/offline.gif") " alt="@T("Moderated") " title="@if(row.UserPart.EmailStatus == UserStatus.Approved) { @T("User is moderated") } else { @T("E-mail validation is pending") }" />
<img class="icon" src="@Href("~/Modules/Orchard.Users/Content/Admin/images/offline.gif") " alt="@T("Moderated") " title="@if(entry.User.EmailStatus == UserStatus.Approved) { @T("User is moderated") } else { @T("E-mail validation is pending") }" />
}
@Html.ActionLink(row.UserPart.UserName, "Edit", new { row.UserPart.Id })
@Html.ActionLink(entry.User.UserName, "Edit", new { entry.User.Id })
</td>
<td>
@row.UserPart.Email
@entry.User.Email
</td>
<td>
@Html.ActionLink(T("Edit").ToString(), "Edit", new { row.UserPart.Id }) |
@Html.ActionLink(T("Remove").ToString(), "Delete", new { row.UserPart.Id }) |
@if(row.UserPart.RegistrationStatus == UserStatus.Pending) {
@Html.ActionLink(T("Approve").ToString(), "Approve", new { row.UserPart.Id })
@Html.ActionLink(T("Edit").ToString(), "Edit", new { entry.User.Id }) |
@Html.ActionLink(T("Remove").ToString(), "Delete", new { entry.User.Id }) |
@if(entry.User.RegistrationStatus == UserStatus.Pending) {
@Html.ActionLink(T("Approve").ToString(), "Approve", new { entry.User.Id })
} else {
@Html.ActionLink(T("Disable").ToString(), "Moderate", new { row.UserPart.Id })
@Html.ActionLink(T("Disable").ToString(), "Moderate", new { entry.User.Id })
}
@if ( row.UserPart.EmailStatus == UserStatus.Pending ) { <text>|</text>
@Html.ActionLink(T("Send challenge E-mail").ToString(), "SendChallengeEmail", new { row.UserPart.Id })
@if ( entry.User.EmailStatus == UserStatus.Pending ) { <text>|</text>
@Html.ActionLink(T("Send challenge E-mail").ToString(), "SendChallengeEmail", new { entry.User.Id })
}
</td>
</tr>
}
userIndex++;
}
</table>
<span class="hint">@T.Plural("No user found", "{1} users found", (int)Model.Pager.TotalItemCount + 1, (int)Model.Pager.TotalItemCount)</span>
@Display(Model.Pager)
</fieldset>
}