2009-11-10 17:33:39 +00:00
|
|
|
using System.Linq;
|
2009-11-10 03:41:01 +00:00
|
|
|
using System.Web.Mvc;
|
2010-08-31 13:20:41 -07:00
|
|
|
using JetBrains.Annotations;
|
2009-12-21 20:29:53 +00:00
|
|
|
using Orchard.ContentManagement;
|
2010-04-15 21:53:32 -07:00
|
|
|
using Orchard.Localization;
|
2009-11-12 03:46:14 +00:00
|
|
|
using Orchard.Security;
|
2010-08-31 13:20:41 -07:00
|
|
|
using Orchard.Settings;
|
2009-11-12 19:19:45 +00:00
|
|
|
using Orchard.UI.Notify;
|
2009-11-10 17:33:39 +00:00
|
|
|
using Orchard.Users.Models;
|
2010-03-02 17:23:45 -08:00
|
|
|
using Orchard.Users.Services;
|
2009-11-10 17:33:39 +00:00
|
|
|
using Orchard.Users.ViewModels;
|
2010-09-01 14:39:28 -07:00
|
|
|
using Orchard.Mvc.Extensions;
|
2009-11-10 03:41:01 +00:00
|
|
|
|
|
|
|
namespace Orchard.Users.Controllers {
|
2010-06-06 14:58:27 -07:00
|
|
|
[ValidateInput(false)]
|
2009-11-19 05:31:39 +00:00
|
|
|
public class AdminController : Controller, IUpdateModel {
|
2009-11-17 05:52:23 +00:00
|
|
|
private readonly IMembershipService _membershipService;
|
2010-03-02 17:23:45 -08:00
|
|
|
private readonly IUserService _userService;
|
2009-11-10 17:33:39 +00:00
|
|
|
|
|
|
|
public AdminController(
|
2009-12-21 01:30:24 +00:00
|
|
|
IOrchardServices services,
|
2010-03-02 17:23:45 -08:00
|
|
|
IMembershipService membershipService,
|
|
|
|
IUserService userService) {
|
2009-12-21 01:30:24 +00:00
|
|
|
Services = services;
|
2009-11-17 05:52:23 +00:00
|
|
|
_membershipService = membershipService;
|
2010-03-02 17:23:45 -08:00
|
|
|
_userService = userService;
|
2009-11-14 05:35:58 +00:00
|
|
|
T = NullLocalizer.Instance;
|
2009-11-10 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
public IOrchardServices Services { get; set; }
|
2009-11-14 05:35:58 +00:00
|
|
|
public Localizer T { get; set; }
|
2010-08-31 13:20:41 -07:00
|
|
|
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
|
2009-11-12 19:56:13 +00:00
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
public ActionResult Index() {
|
2010-01-22 22:11:10 +00:00
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to list users")))
|
2010-01-22 05:25:54 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
var users = Services.ContentManager
|
2010-07-22 23:56:17 -07:00
|
|
|
.Query<UserPart, UserPartRecord>()
|
2009-11-27 04:55:05 +00:00
|
|
|
.Where(x => x.UserName != null)
|
|
|
|
.List();
|
2009-11-26 01:17:48 +00:00
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
var model = new UsersIndexViewModel {
|
|
|
|
Rows = users
|
2010-07-22 23:56:17 -07:00
|
|
|
.Select(x => new UsersIndexViewModel.Row { UserPart = x })
|
2009-12-21 01:30:24 +00:00
|
|
|
.ToList()
|
|
|
|
};
|
2009-11-10 17:33:39 +00:00
|
|
|
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionResult Create() {
|
2010-01-22 22:11:10 +00:00
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")))
|
2010-01-22 05:25:54 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2010-09-02 15:11:33 -07:00
|
|
|
var user = Services.ContentManager.New<IUser>("User");
|
2009-11-27 05:12:52 +00:00
|
|
|
var model = new UserCreateViewModel {
|
2010-09-02 15:11:33 -07:00
|
|
|
User = Services.ContentManager.BuildEditorShape(user)
|
2009-11-27 05:12:52 +00:00
|
|
|
};
|
2009-11-10 17:33:39 +00:00
|
|
|
return View(model);
|
2009-11-10 03:41:01 +00:00
|
|
|
}
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
[HttpPost, ActionName("Create")]
|
2010-03-04 13:25:22 -08:00
|
|
|
public ActionResult CreatePOST(UserCreateViewModel model) {
|
2010-01-22 22:11:10 +00:00
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")))
|
2010-01-22 05:25:54 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
2009-12-21 01:30:24 +00:00
|
|
|
|
2010-09-02 15:11:33 -07:00
|
|
|
var user = Services.ContentManager.New<IUser>("User");
|
|
|
|
model.User = Services.ContentManager.UpdateEditorShape(user, this);
|
2010-03-04 13:25:22 -08:00
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
Services.TransactionManager.Cancel();
|
|
|
|
return View(model);
|
|
|
|
}
|
2009-11-27 05:12:52 +00:00
|
|
|
|
2010-03-02 17:23:45 -08:00
|
|
|
string userExistsMessage = _userService.VerifyUserUnicity(model.UserName, model.Email);
|
2010-03-01 19:06:28 -08:00
|
|
|
if (userExistsMessage != null) {
|
2010-04-16 12:47:06 -07:00
|
|
|
AddModelError("NotUniqueUserName", T(userExistsMessage));
|
2010-03-01 19:06:28 -08:00
|
|
|
}
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
if (model.Password != model.ConfirmPassword) {
|
2010-04-16 12:47:06 -07:00
|
|
|
AddModelError("ConfirmPassword", T("Password confirmation must match"));
|
2009-12-21 01:30:24 +00:00
|
|
|
}
|
|
|
|
|
2010-03-04 13:25:22 -08:00
|
|
|
user = _membershipService.CreateUser(new CreateUserParams(
|
2010-03-02 12:38:54 -08:00
|
|
|
model.UserName,
|
|
|
|
model.Password,
|
|
|
|
model.Email,
|
|
|
|
null, null, true));
|
|
|
|
|
2010-09-02 15:11:33 -07:00
|
|
|
model.User = Services.ContentManager.UpdateEditorShape(user, this);
|
2010-03-02 12:38:54 -08:00
|
|
|
|
2009-11-27 05:12:52 +00:00
|
|
|
if (ModelState.IsValid == false) {
|
2009-12-21 01:30:24 +00:00
|
|
|
Services.TransactionManager.Cancel();
|
2009-11-27 05:12:52 +00:00
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2010-03-01 19:06:28 -08:00
|
|
|
return RedirectToAction("edit", new {user.Id});
|
2009-11-10 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ActionResult Edit(int id) {
|
2010-01-22 22:11:10 +00:00
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")))
|
2010-01-22 05:25:54 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
return View(new UserEditViewModel {
|
2010-09-02 15:11:33 -07:00
|
|
|
User = Services.ContentManager.BuildEditorShape<UserPart>(id)
|
2009-12-21 01:30:24 +00:00
|
|
|
});
|
2009-11-10 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
[HttpPost, ActionName("Edit")]
|
2010-01-22 05:25:54 +00:00
|
|
|
public ActionResult EditPOST(int id) {
|
2010-01-22 22:11:10 +00:00
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")))
|
2010-01-22 05:25:54 +00:00
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
var model = new UserEditViewModel {
|
2010-09-02 15:11:33 -07:00
|
|
|
User = Services.ContentManager.UpdateEditorShape<UserPart>(id, this)
|
2009-12-21 01:30:24 +00:00
|
|
|
};
|
2009-11-13 00:19:43 +00:00
|
|
|
|
2010-03-05 12:12:14 -08:00
|
|
|
TryUpdateModel(model);
|
|
|
|
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
Services.TransactionManager.Cancel();
|
|
|
|
return View(model);
|
|
|
|
}
|
2009-12-21 01:30:24 +00:00
|
|
|
|
2010-03-02 17:23:45 -08:00
|
|
|
model.User.Item.NormalizedUserName = model.UserName.ToLower();
|
|
|
|
|
|
|
|
string userExistsMessage = _userService.VerifyUserUnicity(id, model.UserName, model.Email);
|
2010-03-01 19:06:28 -08:00
|
|
|
if (userExistsMessage != null) {
|
2010-04-16 12:47:06 -07:00
|
|
|
AddModelError("NotUniqueUserName", T(userExistsMessage));
|
2010-03-01 19:06:28 -08:00
|
|
|
}
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
Services.TransactionManager.Cancel();
|
2009-11-10 17:33:39 +00:00
|
|
|
return View(model);
|
|
|
|
}
|
2009-11-14 02:35:43 +00:00
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
Services.Notifier.Information(T("User information updated"));
|
2009-11-10 17:33:39 +00:00
|
|
|
return RedirectToAction("Edit", new { id });
|
|
|
|
}
|
2009-11-12 03:46:14 +00:00
|
|
|
|
2010-01-27 20:39:35 +00:00
|
|
|
public ActionResult Delete(int id) {
|
|
|
|
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")))
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
Services.ContentManager.Remove(Services.ContentManager.Get(id));
|
|
|
|
|
|
|
|
Services.Notifier.Information(T("User deleted"));
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
|
2010-09-01 18:18:53 -07:00
|
|
|
public ActionResult SendChallengeEmail(int id) {
|
|
|
|
if ( !Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")) )
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
var user = Services.ContentManager.Get(id);
|
|
|
|
|
|
|
|
if ( user != null ) {
|
|
|
|
string challengeToken = _membershipService.GetEncryptedChallengeToken(user.As<UserPart>());
|
|
|
|
_membershipService.SendChallengeEmail(user.As<UserPart>(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new {Area = "Orchard.Users", token = challengeToken})));
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.Notifier.Information(T("Challenge email sent"));
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
|
2010-08-31 13:20:41 -07:00
|
|
|
public ActionResult Approve(int id) {
|
|
|
|
if ( !Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")) )
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
var user = Services.ContentManager.Get(id);
|
|
|
|
|
|
|
|
if ( user != null ) {
|
|
|
|
user.As<UserPart>().RegistrationStatus = UserStatus.Approved;
|
|
|
|
Services.Notifier.Information(T("User approved"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionResult Moderate(int id) {
|
|
|
|
if ( !Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")) )
|
|
|
|
return new HttpUnauthorizedResult();
|
|
|
|
|
|
|
|
var user = Services.ContentManager.Get(id);
|
|
|
|
|
|
|
|
if ( user != null ) {
|
|
|
|
if ( CurrentSite.SuperUser.Equals(user.As<UserPart>().UserName) ) {
|
|
|
|
Services.Notifier.Error(T("Super user can't be moderated"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
user.As<UserPart>().RegistrationStatus = UserStatus.Pending;
|
|
|
|
Services.Notifier.Information(T("User moderated"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
|
2009-11-19 05:31:39 +00:00
|
|
|
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
|
2009-11-13 00:19:43 +00:00
|
|
|
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
|
|
|
|
}
|
2010-04-16 12:47:06 -07:00
|
|
|
|
|
|
|
public void AddModelError(string key, LocalizedString errorMessage) {
|
|
|
|
ModelState.AddModelError(key, errorMessage.ToString());
|
|
|
|
}
|
2009-11-10 03:41:01 +00:00
|
|
|
}
|
2009-11-10 17:33:39 +00:00
|
|
|
|
2009-11-10 03:41:01 +00:00
|
|
|
}
|