/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Json;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Everest.CmsServices.Services;
using Everest.CmsServices.Models;
using Everest.Library;
namespace Everest.CmsServices.Controllers{
public class RoleController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region StandardAction
[PermissionFilter(Permission = FolderType.Roles)]
public ActionResult GetRoles()
{
int start, limit;
EnsurePaging(out start, out limit);
var roles = dataContext.QueryRoles(Membership.ApplicationName).Select(r =>
new
{
r.RoleId,
RoleName = r.RoleName
});
return Json(new ExtJsonReaderObject(roles.OrderBy(r => r.RoleName).Skip(start).Take(limit), roles.Count()));
}
[PermissionFilter(Permission = FolderType.Roles)]
public ActionResult GetRole()
{
JsonResultData formResult = new JsonResultData();
string roleId = Request.Form["RoleId"];
object data = GetRoleDetails(new Guid(roleId));
formResult.success = true;
formResult.data = data;
return Json(formResult);
}
private object GetRoleDetails(Guid roleId)
{
var role = dataContext.QueryRole(roleId).Select(r => new
{
r.RoleId,
RoleName = r.RoleName,
FormTitle = r.RoleName,
Description = r.Description,
Permissions = r.Cms_Permission.OrderBy(p => p.Order).Select(p => new QueriedPermission()
{
PermissionName = p.PermissionName,
View = p.View
})
});
var data = role.First();
var defaultPermissions = this.GetDefaultPermissions();
foreach (var permission in defaultPermissions)
{
var existPermission = data.Permissions.Where(p => p.PermissionName == permission.PermissionName).FirstOrDefault();
if (existPermission != null)
{
permission.View = existPermission.View;
}
}
return new
{
data.RoleId,
data.RoleName,
data.FormTitle,
data.Description,
Permissions = defaultPermissions
};
}
/// <summary>
/// Loads the default permissions.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Roles)]
public ActionResult LoadDefaultPermissions()
{
var defaultPermissions = GetDefaultPermissions();
return Json(new ExtJsonReaderObject(defaultPermissions, defaultPermissions.Count()));
}
/// <summary>
/// Gets the default permissions.
/// </summary>
/// <returns></returns>
private IEnumerable<QueriedPermission> GetDefaultPermissions()
{
List<QueriedPermission> defaultPermission = new List<QueriedPermission>()
{
new QueriedPermission(){PermissionName = FolderType.Schedule.ToString()},
new QueriedPermission(){PermissionName = FolderType.Users.ToString()},
new QueriedPermission(){PermissionName = FolderType.Roles.ToString()},
new QueriedPermission(){PermissionName = FolderType.WebEvent.ToString()},
new QueriedPermission(){PermissionName= FolderType.SitePackage.ToString()},
new QueriedPermission(){PermissionName= FolderType.KoobooStore.ToString()},
new QueriedPermission(){PermissionName= FolderType.CheckUpdates.ToString()},
new QueriedPermission(){PermissionName = FolderType.Workflow.ToString()},
new QueriedPermission(){PermissionName = FolderType.SearchSetting.ToString()},
new QueriedPermission(){PermissionName = FolderType.Rss.ToString()},
new QueriedPermission(){PermissionName = FolderType.TextSchema.ToString()},
new QueriedPermission(){PermissionName = FolderType.BinarySchema.ToString()},
new QueriedPermission(){PermissionName = FolderType.ValidatorGroup.ToString()},
new QueriedPermission(){PermissionName = FolderType.Validator.ToString()},
new QueriedPermission(){PermissionName = FolderType.ContentTemplate.ToString()},
new QueriedPermission(){PermissionName = FolderType.LayoutTemplate.ToString()},
new QueriedPermission(){PermissionName = FolderType.TextResource.ToString()},
new QueriedPermission(){PermissionName = FolderType.BinaryResource.ToString()},
//new QueriedPermission(){PermissionName = FolderType.WebForm.ToString()},
new QueriedPermission(){PermissionName =FolderType.PagePlugin.ToString()},
new QueriedPermission(){PermissionName = FolderType.Module.ToString()},
new QueriedPermission(){PermissionName = FolderType.StaticCode.ToString()},
new QueriedPermission(){PermissionName = FolderType.Application.ToString()},
new QueriedPermission(){PermissionName = FolderType.Content.ToString()},
new QueriedPermission(){PermissionName = FolderType.ContentFolder.ToString()},
new QueriedPermission(){PermissionName = FolderType.Page.ToString()},
};
return defaultPermission;
}
[PermissionFilter(Permission = FolderType.Roles)]
public ActionResult DeleteRole(string[] roleName)
{
JsonResultData formResult = new JsonResultData() { success = true };
foreach (var role in roleName)
{
Roles.DeleteRole(role, true);
}
return Json(formResult);
}
/// <summary>
/// Submits the role.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Roles)]
public ActionResult SubmitRole(bool add, bool closeForm)
{
JsonResultData resultData = new JsonResultData();
string application = Request.Form["application"];
aspnet_Roles role;
try
{
if (add)
{
role = new aspnet_Roles();
role.RoleId = Guid.NewGuid();
role.aspnet_Applications = dataContext.QueryApplication(application).First();
}
else
{
var roleId = new Guid(Request.Form["oldData.RoleId"]);
role = (from r in dataContext.aspnet_Roles
where r.RoleId == roleId
select r).First();
role.ClearPermission(dataContext);
}
role.RoleName = Request.Form["RoleName"];
role.LoweredRoleName = role.RoleName.ToLower();
role.Description = Request.Form["Description"];
Cms_Permission[] permissions = Request.Form["Permissions"].DeserializeJSON<Cms_Permission[]>();
foreach (var item in permissions)
{
role.Cms_Permission.Add(item);
}
dataContext.SaveChanges();
if (closeForm == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetRoleDetails(role.RoleId);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
return Json(resultData);
}
#endregion
#region Combobox
/// <summary>
/// Gets the roles for combo box.
/// </summary>
/// <returns></returns>
public ActionResult GetRolesForComboBox()
{
var rolesQuery = dataContext.aspnet_Roles;
var items = rolesQuery.ToComboboxItems(r => r.RoleName, r => r.RoleName);
return Json(new ExtJsonReaderObject(items, items.Count));
}
public ActionResult GetAuthorizerString()
{
var dataContext = EverestCmsEntities.GetDataContext();
var rolesQuery = dataContext.aspnet_Roles;
var items = rolesQuery.ToComboboxItems(r => r.RoleName, r => r.RoleName);
items.Insert(0, new ComboboxItem("?", "?"));
items.Insert(1, new ComboboxItem("*", "*"));
return Json(new ExtJsonReaderObject(items, items.Count));
}
#endregion
public ActionResult GetPermission(string application, string permissionName)
{
PermissionService permissionService = UnityManager.Resolve<PermissionService>();
return Json(new JsonResultData() { data = permissionService.IsAllowed(User.Identity.Name, permissionName, application, ActionType.View) });
}
}
}
|