2009-11-12 03:46:14 +00:00
|
|
|
using System;
|
2009-11-10 17:33:39 +00:00
|
|
|
using System.Linq;
|
2009-11-10 03:41:01 +00:00
|
|
|
using System.Web.Mvc;
|
2009-11-10 17:33:39 +00:00
|
|
|
using Orchard.Data;
|
2009-11-14 02:35:43 +00:00
|
|
|
using Orchard.Localization;
|
2009-11-10 17:33:39 +00:00
|
|
|
using Orchard.Models;
|
2009-11-13 00:19:43 +00:00
|
|
|
using Orchard.Models.Driver;
|
2009-11-12 03:46:14 +00:00
|
|
|
using Orchard.Security;
|
2009-11-12 19:19:45 +00:00
|
|
|
using Orchard.UI.Notify;
|
2009-11-10 17:33:39 +00:00
|
|
|
using Orchard.Users.Models;
|
|
|
|
using Orchard.Users.ViewModels;
|
2009-11-10 03:41:01 +00:00
|
|
|
|
|
|
|
namespace Orchard.Users.Controllers {
|
2009-11-14 02:35:43 +00:00
|
|
|
|
2009-11-19 05:31:39 +00:00
|
|
|
public class AdminController : Controller, IUpdateModel {
|
2009-12-21 01:30:24 +00:00
|
|
|
|
2009-11-17 05:52:23 +00:00
|
|
|
private readonly IMembershipService _membershipService;
|
2009-11-10 17:33:39 +00:00
|
|
|
|
|
|
|
public AdminController(
|
2009-12-21 01:30:24 +00:00
|
|
|
IOrchardServices services,
|
|
|
|
IMembershipService membershipService) {
|
|
|
|
Services = services;
|
2009-11-17 05:52:23 +00:00
|
|
|
_membershipService = membershipService;
|
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; }
|
2009-11-12 19:56:13 +00:00
|
|
|
|
2009-11-26 01:17:48 +00:00
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
public ActionResult Index() {
|
|
|
|
var users = Services.ContentManager
|
|
|
|
.Query<User, UserRecord>(Models.User.ContentType.Name)
|
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
|
|
|
|
.Select(x => new UsersIndexViewModel.Row { User = x })
|
|
|
|
.ToList()
|
|
|
|
};
|
2009-11-10 17:33:39 +00:00
|
|
|
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionResult Create() {
|
2009-12-21 01:30:24 +00:00
|
|
|
var user = Services.ContentManager.New<IUser>(Models.User.ContentType.Name);
|
2009-11-27 05:12:52 +00:00
|
|
|
var model = new UserCreateViewModel {
|
2009-12-21 01:30:24 +00:00
|
|
|
User = Services.ContentManager.BuildEditorModel(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")]
|
|
|
|
public ActionResult _Create() {
|
|
|
|
|
|
|
|
var model = new UserCreateViewModel();
|
|
|
|
UpdateModel(model);
|
2009-11-27 05:12:52 +00:00
|
|
|
|
2009-11-17 05:52:23 +00:00
|
|
|
var user = _membershipService.CreateUser(new CreateUserParams(
|
|
|
|
model.UserName,
|
|
|
|
model.Password,
|
|
|
|
model.Email,
|
|
|
|
null, null, true));
|
2009-12-21 01:30:24 +00:00
|
|
|
|
|
|
|
model.User = Services.ContentManager.UpdateEditorModel(user, this);
|
|
|
|
|
|
|
|
if (model.Password != model.ConfirmPassword) {
|
|
|
|
AddModelError("ConfirmPassword", T("Password confirmation must match"));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-11-10 17:33:39 +00:00
|
|
|
return RedirectToAction("edit", new { user.Id });
|
|
|
|
}
|
|
|
|
|
|
|
|
public ActionResult Edit(int id) {
|
2009-12-21 01:30:24 +00:00
|
|
|
return View(new UserEditViewModel {
|
|
|
|
User = Services.ContentManager.BuildEditorModel<User>(id)
|
|
|
|
});
|
2009-11-10 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
[HttpPost, ActionName("Edit")]
|
|
|
|
public ActionResult _Edit(int id) {
|
|
|
|
var model = new UserEditViewModel {
|
|
|
|
User = Services.ContentManager.UpdateEditorModel<User>(id, this)
|
|
|
|
};
|
2009-11-13 00:19:43 +00:00
|
|
|
|
2009-12-21 01:30:24 +00:00
|
|
|
// apply additional model properties that were posted on form
|
|
|
|
UpdateModel(model);
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
2009-12-21 01:30:24 +00:00
|
|
|
public void AddModelError(string key, LocalizedString errorMessage) {
|
2009-12-08 06:13:08 +00:00
|
|
|
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
|
|
|
}
|