Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created August 17, 2012 07:37
Show Gist options
  • Save loosechainsaw/3376786 to your computer and use it in GitHub Desktop.
Save loosechainsaw/3376786 to your computer and use it in GitHub Desktop.
Updated MemberHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Runtime.InteropServices;
using OpenRasta.Web;
using umbraco.cms.businesslogic.member;
using uREST.Core.Attributes;
using uREST.Core.Handlers;
using uREST.CMS.Extensions;
namespace uREST.CMS.Handlers
{
[UmbracoAuthorize(Applications = "member")]
public class MemberHandler : AbstractHandler
{
public MemberHandler(ICommunicationContext context)
: base(context)
{ }
// GET (LIST)
[HttpOperation(ForUriName = "MemberList")]
public OperationResult GetMemberList(
[Optional] string username,
[Optional] string role,
[Optional] string memberTypeAlias)
{
var memberList = new List<Member>();
memberList.AddRange(!string.IsNullOrEmpty(username)
? Member.GetMemberByName(username, false)
: Member.GetAllAsList());
if (!string.IsNullOrEmpty(role))
memberList = memberList.Where(member => Roles.IsUserInRole(member.LoginName, role)).ToList();
if (!string.IsNullOrEmpty(memberTypeAlias))
memberList = memberList.Where(member => member.ContentType.Alias == memberTypeAlias).ToList();
return new OperationResult.OK { ResponseResource = memberList.ToResourceList() };
}
// GET (SINGLE)
[HttpOperation(ForUriName = "Member")]
public OperationResult GetMember(
[Optional] int id,
[Optional] string username)
{
if(id == 0 && string.IsNullOrEmpty(username))
return new OperationResult.BadRequest();
Member member = null;
try
{
member = id > 0 ?
new Member(id) :
Member.GetMemberFromLoginName(username);
}
catch
{ }
if (member == null)
return new OperationResult.NotFound();
return new OperationResult.OK { ResponseResource = member.ToResource() };
}
// CREATE
[HttpOperation(ForUriName = "MemberList")]
public OperationResult PostMember(Resources.Member resource)
{
if (resource.Id > 0 || string.IsNullOrEmpty(resource.MemberTypeAlias) || string.IsNullOrEmpty(resource.Username) || string.IsNullOrEmpty(resource.Email))
return new OperationResult.BadRequest();
var memberType = MemberType.GetByAlias(resource.MemberTypeAlias);
if (memberType == null)
return new OperationResult.BadRequest();
if (string.IsNullOrEmpty(resource.DisplayName))
resource.DisplayName = resource.Username;
var member = Member.MakeNew(resource.Username, resource.Email, memberType, AuthenticatedUser);
member.Text = resource.DisplayName;
member.Password = resource.Password;
if (resource.Properties != null && resource.Properties.Count > 0)
{
foreach (var propResource in resource.Properties)
{
var prop = member.getProperty(propResource.Key);
if (prop != null)
prop.Value = propResource.Value;
}
}
if (resource.Roles != null && resource.Roles.Any())
{
foreach (var role in resource.Roles)
Roles.AddUserToRole(member.User.LoginName, role);
}
member.Save();
return new OperationResult.Created { ResponseResource = member.ToResource() };
}
// UPDATE
[HttpOperation(ForUriName = "Member")]
public OperationResult PutMember(
[Optional] int id,
[Optional] string username,
Resources.Member resource)
{
if (id == 0 && string.IsNullOrEmpty(username))
return new OperationResult.BadRequest();
Member member = null;
try
{
member = id > 0 ?
new Member(id) :
Member.GetMemberFromLoginName(username);
}
catch
{ }
if (member == null)
return new OperationResult.NotFound();
member.Text = resource.DisplayName;
member.LoginName = resource.Username;
member.Email = resource.Email;
if (!string.IsNullOrEmpty(resource.Password))
member.Password = resource.Password;
if (resource.Properties != null && resource.Properties.Count > 0)
{
foreach (var propResource in resource.Properties)
{
var prop = member.getProperty(propResource.Key);
if (prop != null)
prop.Value = propResource.Value;
}
}
foreach (var role in Roles.GetRolesForUser(member.User.LoginName))
{
Roles.RemoveUserFromRole(member.LoginName, role);
}
if (resource.Roles != null && resource.Roles.Any())
{
foreach (var role in resource.Roles)
Roles.AddUserToRole(member.User.LoginName, role);
}
member.Save();
return new OperationResult.Created { ResponseResource = member.ToResource() };
}
// DELETE
[HttpOperation(ForUriName = "Member")]
public OperationResult DeleteMember(
[Optional] int id,
[Optional] string username)
{
if (id == 0 && string.IsNullOrEmpty(username))
return new OperationResult.BadRequest();
Member member = null;
try
{
member = id > 0 ?
new Member(id) :
Member.GetMemberFromLoginName(username);
}
catch
{ }
if (member == null)
return new OperationResult.NotFound();
try
{
member.delete();
return new OperationResult.NoContent();
}
catch
{
return new OperationResult.InternalServerError();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment