mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
- Create/Read/Update/Delete actions for the Roles module.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4039504
This commit is contained in:
@@ -1,43 +1,94 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Data;
|
||||
using Orchard.Notify;
|
||||
using Orchard.Roles.Models;
|
||||
using Orchard.Roles.Services;
|
||||
using Orchard.Roles.ViewModels;
|
||||
|
||||
namespace Orchard.Roles.Controllers {
|
||||
[ValidateInput(false)]
|
||||
public class AdminController : Controller {
|
||||
private readonly IRepository<RoleRecord> _roleRepository;
|
||||
private readonly IRoleService _roleService;
|
||||
private readonly INotifier _notifier;
|
||||
|
||||
public AdminController(IRepository<RoleRecord> roleRepository, INotifier notifier) {
|
||||
_roleRepository = roleRepository;
|
||||
public AdminController(IRoleService roleService, INotifier notifier) {
|
||||
_roleService = roleService;
|
||||
_notifier = notifier;
|
||||
}
|
||||
|
||||
public ActionResult Index() {
|
||||
var model = new RolesIndexViewModel {
|
||||
Rows = _roleRepository.Fetch(x => x.Name != null)
|
||||
.Select(x => new RolesIndexViewModel.Row {
|
||||
Role = x
|
||||
})
|
||||
.ToList()
|
||||
};
|
||||
var model = new RolesIndexViewModel { Rows = _roleService.GetRoles() as IList<RoleRecord> };
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
//TODO: NYI
|
||||
[AcceptVerbs(HttpVerbs.Post)]
|
||||
public ActionResult Index(FormCollection input) {
|
||||
try {
|
||||
foreach (string key in input.Keys) {
|
||||
if (key.StartsWith("Checkbox.") && input[key] == "true") {
|
||||
int roleId = Convert.ToInt32(key.Substring("Checkbox.".Length));
|
||||
_roleService.DeleteRole(roleId);
|
||||
}
|
||||
}
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error("Deleting Role failed: " + exception.Message);
|
||||
return View();
|
||||
}
|
||||
}
|
||||
|
||||
public ActionResult Create() {
|
||||
var model = new RoleCreateViewModel();
|
||||
return View(model);
|
||||
}
|
||||
|
||||
//TODO: NYI
|
||||
[AcceptVerbs(HttpVerbs.Post)]
|
||||
public ActionResult Create(FormCollection input) {
|
||||
var viewModel = new RoleCreateViewModel();
|
||||
try {
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
_roleService.CreateRole(viewModel.Name);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error("Creating Role failed: " + exception.Message);
|
||||
return View(viewModel);
|
||||
}
|
||||
}
|
||||
|
||||
public ActionResult Edit(int id) {
|
||||
var model = new RoleEditViewModel();
|
||||
var role = _roleService.GetRole(id);
|
||||
if (role == null) {
|
||||
//TODO: Error message
|
||||
throw new HttpException(404, "page with id " + id + " was not found");
|
||||
}
|
||||
var model = new RoleEditViewModel { Name = role.Name, Id = role.Id };
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[AcceptVerbs(HttpVerbs.Post)]
|
||||
public ActionResult Edit(FormCollection input) {
|
||||
var viewModel = new RoleEditViewModel();
|
||||
try {
|
||||
UpdateModel(viewModel, input.ToValueProvider());
|
||||
// Save
|
||||
if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Save"])) {
|
||||
_roleService.UpdateRole(viewModel.Id, viewModel.Name);
|
||||
}
|
||||
else if (!String.IsNullOrEmpty(HttpContext.Request.Form["submit.Delete"])) {
|
||||
_roleService.DeleteRole(viewModel.Id);
|
||||
}
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
catch (Exception exception) {
|
||||
_notifier.Error("Editing media file failed: " + exception.Message);
|
||||
return View(viewModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user