/*
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.Data;
using Everest.Library.Data.Entity;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
namespace Everest.CmsServices.Controllers{
public class CmsUserController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region Standard Actions
/// <summary>
/// Gets the users.
/// </summary>
/// <param name="start">The start.</param>
/// <param name="limit">The limit.</param>
/// <returns></returns>
public ActionResult GetUsers()
{
int start, limit;
EnsurePaging(out start, out limit);
var users = dataContext.QueryMemberships(Membership.ApplicationName).Select(m => new
{
UserId = m.UserId,
UserName = m.aspnet_Users.UserName,
Email = m.Email,
CreateDate = m.CreateDate,
IsApproved = m.IsApproved,
IsLockedOut = m.IsLockedOut,
LastLoginDate = m.LastLoginDate,
LastLockoutDate = m.LastLockoutDate,
FailedPasswordAttemptCount = m.FailedPasswordAttemptCount,
FailedPasswordAttemptWindowStart = m.FailedPasswordAttemptWindowStart,
FailedPasswordAnswerAttemptCount = m.FailedPasswordAnswerAttemptCount,
FailedPasswordAnswerAttemptWindowStart = m.FailedPasswordAnswerAttemptWindowStart
});
string userName = Request.Form["UserName"];
if (!StringExtensions.IsNullOrEmptyTrim(userName))
{
users = users.Where(u => u.UserName.Contains(userName));
}
return Json(new ExtJsonReaderObject(users.OrderBy(p => p.CreateDate).Skip(start).Take(limit).ToArray(), users.Count()));
}
/// <summary>
/// Users the details.
/// </summary>
/// <returns></returns>
public ActionResult UserDetails()
{
string userName = Request.Form["UserName"];
object data = GetUserDetails(userName);
return Json(new JsonResultData() { success = true, data = data });
}
/// <summary>
/// Gets the user details.
/// </summary>
/// <param name="userName">Name of the user.</param>
/// <returns></returns>
private object GetUserDetails(string userName)
{
var roleQuery = (from r in dataContext.aspnet_Roles
where r.aspnet_Users.Any(u => u.UserName == userName)
select r.RoleName);
string roles = roleQuery.JoinString(",");
var applicationNamesList = dataContext.QueryApplicationsByUser(userName).Select(a => a.ApplicationName).OrderBy(s => s).ToList();
if (applicationNamesList.Contains(CmsGlobal.RootApplicationName))
{
applicationNamesList.Remove(CmsGlobal.RootApplicationName);
applicationNamesList.Insert(0, CmsGlobal.RootApplicationName);
}
var applicationNames = applicationNamesList.JoinString(",");
var query = from m in dataContext.aspnet_Membership
where m.aspnet_Users.UserName == userName
select new
{
UserName = m.aspnet_Users.UserName,
FormTitle = m.aspnet_Users.UserName,
Email = m.Email,
ApplicationName = applicationNames,
IsApproved = m.IsApproved,
PasswordQuestion = m.PasswordQuestion,
PasswordAnswer = m.PasswordAnswer,
IsLockedOut = m.IsLockedOut,
Roles = roles
};
object data = query.FirstOrDefault();
return data;
}
/// <summary>
/// Submits the user.
/// </summary>
/// <param name="add">if set to <c>true</c> [add].</param>
/// <param name="closeForm">if set to <c>true</c> [close form].</param>
/// <returns></returns>
public ActionResult SubmitUser(bool add, bool closeForm)
{
JsonResultData resultData = new JsonResultData();
resultData.success = false;
string userName;
if (add)
{
userName = Request.Form["UserName"];
if (StringExtensions.IsNullOrEmptyTrim(userName))
{
resultData.AddError("UserName", string.Format(Resources.FieldIsRequired, "UserName"));
goto End;
}
if (dataContext.QueryUser(userName).Exists())
{
resultData.AddError("UserName", string.Format(Resources.UserIsAlreadyExists, "UserName"));
goto End;
}
string email = Request.Form["Email"];
if (StringExtensions.IsNullOrEmptyTrim(email))
{
resultData.AddError("Email", string.Format(Resources.FieldIsRequired, "Email"));
goto End;
}
string password = Request.Form["Password"];
if (StringExtensions.IsNullOrEmptyTrim(password))
{
resultData.AddError("Password", string.Format(Resources.FieldIsRequired, "Password"));
goto End;
}
bool isApproved = false;
if (StringExtensions.IsTrue(Request.Form["IsApproved"]))
{
isApproved = true;
}
string passwordQuestion = StringExtensions.IsNullOrEmptyTrim(Request.Form["PasswordQuestion"]) ? null : Request.Form["PasswordQuestion"];
string passwordAnswer = StringExtensions.IsNullOrEmptyTrim(Request.Form["PasswordAnswer"]) ? null : Request.Form["PasswordAnswer"];
MembershipCreateStatus status;
Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, isApproved, null, out status);
switch (status)
{
case MembershipCreateStatus.Success:
resultData.success = true;
string applicationName = Request.Form["ApplicationName"];
SetUserToApplications(userName, applicationName, dataContext);
if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["Roles"]))
{
string[] roles = Request.Form["Roles"].Split(',');
if (roles.Length > 0)
{
//RoleProvider roleProvider = GetRoleProvider(applicationName);
Roles.AddUsersToRoles(new string[] { userName }, roles);
}
}
dataContext.SaveChanges();
break;
default:
resultData.success = false;
resultData.AddError("UserName", status.ToString());
break;
}
}
else
{
resultData.success = true;
userName = Request.Form["oldData.UserName"];
string applicationName = Request.Form["ApplicationName"];
if (StringExtensions.IsNullOrEmptyTrim(userName))
{
throw new ArgumentNullException("oldData.UserName");
}
var query = from m in dataContext.aspnet_Membership
where m.aspnet_Users.UserName == userName && m.aspnet_Applications.ApplicationName == Membership.ApplicationName
select m;
aspnet_Membership user = query.First();
if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["Email"]))
{
user.Email = Request.Form["Email"];
user.LoweredEmail = user.Email.ToLower();
}
if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["PasswordQuestion"]))
{
user.PasswordQuestion = Request.Form["PasswordQuestion"];
}
if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["PasswordAnswer"]))
{
user.PasswordAnswer = Request.Form["PasswordAnswer"];
}
user.IsApproved = StringExtensions.IsTrue(Request.Form["IsApproved"]);
user.IsLockedOut = StringExtensions.IsTrue(Request.Form["IsLockedOut"]);
SetUserToApplications(userName, applicationName, dataContext);
dataContext.SaveChanges();
if (!StringExtensions.IsNullOrEmptyTrim(Request.Form["Roles"]))
{
string[] roles = Request.Form["Roles"].Split(',');
if (roles.Length > 0)
{
string[] roleNames = Roles.GetRolesForUser(userName);
if (roleNames != null && roleNames.Length > 0)
{
Roles.RemoveUserFromRoles(userName, roleNames);
}
Roles.AddUsersToRoles(new string[] { userName }, roles);
}
}
}
End:
if (closeForm == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetUserDetails(userName);
}
return Json(resultData);
}
/// <summary>
/// Sets the user to applications.
/// </summary>
/// <param name="userName">Name of the user.</param>
/// <param name="applicationName">Name of the application.</param>
/// <param name="dataContext">The data context.</param>
private void SetUserToApplications(string userName, string applicationName, IEverestCmsDataContext dataContext)
{
var user = dataContext.QueryUser(userName).First();
user.UsersInCmsApplication.Load();
user.UsersInCmsApplication.Clear();
string[] apps = applicationName.Split(',');
var appQuery = dataContext.QueryApplications(apps);
foreach (var app in appQuery.ToArray())
{
user.UsersInCmsApplication.Add(app);
}
}
/// <summary>
/// Deletes the user.
/// </summary>
/// <returns></returns>
public ActionResult DeleteUser(string[] userName)
{
JsonResultData formResult = new JsonResultData();
formResult.success = true;
foreach (var user in userName)
{
Membership.DeleteUser(user, true);
}
return Json(formResult);
}
#endregion
#region Profile
public ActionResult GetProfile()
{
JsonResultData jsonResultData = new JsonResultData();
jsonResultData.data = GetProfileData();
return Json(jsonResultData);
}
private object GetProfileData()
{
var data = new
{
Language = Profile.Language,
ShowBase = Profile.ShowBase,
Profile.Theme
};
return data;
}
public ActionResult SubmitProfile(string language, string theme, bool closeForm)
{
JsonResultData jsonResultData = new JsonResultData() { closeForm = closeForm };
Profile.Language = language;
Profile.Theme = theme;
Profile.Created = true;
Profile.Save();
if (!closeForm)
{
jsonResultData.data = GetProfileData();
}
return Json(jsonResultData);
}
#endregion
}
}
|