Taylohtio/GeneralSSO/GeneralSSO.Server/Layouts/Taloyhtio/OAuth/API/DataApi.svc.cs

84 lines
3.2 KiB
C#

using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.Text;
using System.Web;
using Microsoft.Practices.ServiceLocation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Taloyhtio.GeneralSSO.Server.CodeFiles.Common;
using Taloyhtio.GeneralSSO.Server.CodeFiles.Entities;
using Taloyhtio.GeneralSSO.Server.CodeFiles.Infrastructure.WCF;
using Taloyhtio.GeneralSSO.Server.CodeFiles.Services;
using System;
using System.Configuration;
namespace Taloyhtio.GeneralSSO.Server
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DataApi : IDataApi
{
private ILogger logger;
public DataApi()
{
this.logger = ServiceLocator.Current.GetInstance<ILogger>();
}
public UserRoles GetRoles(string siteUrl)
{
try
{
if (string.IsNullOrEmpty(siteUrl))
{
throw new HttpException((int)HttpStatusCode.BadRequest, "Site url is empty");
}
else if (!SPUrlUtility.IsUrlRelative(siteUrl))
{
throw new HttpException((int)HttpStatusCode.BadRequest, "Site url should be relative");
}
string userId = this.getUserId();
if (string.IsNullOrEmpty(userId))
{
throw new Exception("User id can't be determined");
}
var rolesProvider = ServiceLocator.Current.GetInstance<IRolesProvider>();
//string authority = OperationContext.Current.IncomingMessageProperties.Via.GetLeftPart(UriPartial.Authority);
string authority = ConfigurationManager.AppSettings["PMCBaseAddress"];
siteUrl = SPUrlUtility.CombineUrl(authority, siteUrl);
using (var site = new SPSite(siteUrl))
{
string loginName = string.Format("{0}:{1}", ConfigurationManager.AppSettings["MembershipProviderName"], userId);
var userRoles = rolesProvider.GetRoles(site, loginName);
if (userRoles == null)
{
throw new Exception("User roles can't be determined");
}
return userRoles.Value;
}
}
catch (Exception x)
{
this.logger.Error(Constants.LogComponents.RESOURCE_SERVER, string.Format("Error occured during retrieving roles for user '{0}' in site '{1}':\n{2}",
this.getUserId(), siteUrl, x.ToInfo()));
throw;
}
}
private string getUserId()
{
if (OperationContext.Current == null || OperationContext.Current.ServiceSecurityContext == null ||
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity == null)
{
return string.Empty;
}
return OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
}
}
}