Taylohtio/CondoUpdate/CondoUpdate.ChangeGeneralUs.../UpdaterImpl.cs

162 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.ChangeGeneralUserIdPasswords
{
public class UpdaterImpl : ICondoUpdater
{
private const string CONDO_PREFIX = "as oy ";
private const string KEY_CONDO_SHORT_NAME = "CondoShortName";
private const string GENERAL_LANDLORD_NAME_TEMPLATE = "{0} osakas";
private const string USER_TYPE_TENANT = "Tenant";
private const string USER_TYPE_LANDLORD = "Landlord";
private readonly Guid FEATURE_ID = new Guid("{7AF92686-156F-4750-9D03-E842D81FBAFA}");
private const string PWD_KEY = "passwords";
public event EventHandler<LogEventArgs> OnNotify;
public void Update(object args)
{
string url = args as string;
if (string.IsNullOrEmpty(url))
{
this.warn("Url is empty");
return;
}
try
{
this.updateImpl(url);
}
catch (Exception x)
{
this.error("Error occured during updating of Condo '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace);
}
}
private void updateImpl(string url)
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (!site.Features.Cast<SPFeature>().Any(f => f.DefinitionId == FEATURE_ID))
{
site.Features.Add(FEATURE_ID);
}
string tenantPwd, landlordPwd;
getPasswords(out tenantPwd, out landlordPwd);
// changing of password is only possible in context of the target site, so membership provider won't be null
string tenantUserName = this.getGeneralTentantUserName(web);
sendRequest(web, tenantUserName, USER_TYPE_TENANT, tenantPwd);
string landlordUserName = this.getGeneralLandlordUserName(web);
sendRequest(web, landlordUserName, USER_TYPE_LANDLORD, landlordPwd);
}
}
}
private void getPasswords(out string tenantPwd, out string landlordPwd)
{
tenantPwd = landlordPwd = string.Empty;
string pwd = ConfigurationManager.AppSettings[PWD_KEY];
if (string.IsNullOrEmpty(pwd))
{
return;
}
var parts = pwd.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
return;
}
else if (parts.Length == 1)
{
tenantPwd = parts[0];
return;
}
tenantPwd = parts[0];
landlordPwd = parts[1];
}
private void sendRequest(SPWeb web, string userName, string userType, string pwd)
{
string handlerUrl = string.Format("{0}?UserName={1}&UserType={2}&Pwd={3}",
SPUrlUtility.CombineUrl(web.Url, "_layouts/15/Taloyhtio/GeneralUserIdPasswords/ChangePwd.ashx"), HttpUtility.UrlDecode(userName),
userType, HttpUtility.UrlDecode(pwd));
var request = (HttpWebRequest)WebRequest.Create(handlerUrl);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
this.error(string.Format("Error occured during changing the password: response code is '{0}' (expected is '{1}')",
response.StatusCode, HttpStatusCode.Redirect));
}
}
private string getCondoShortName(SPWeb web)
{
if (web.AllProperties.ContainsKey(KEY_CONDO_SHORT_NAME))
{
return web.AllProperties[KEY_CONDO_SHORT_NAME] as string;
}
if (web.Title.ToLower().StartsWith(CONDO_PREFIX.ToLower()))
{
return web.Title.Substring(CONDO_PREFIX.Length);
}
return string.Empty;
}
private string getGeneralTentantUserName(SPWeb condoWeb)
{
string shortName = getCondoShortName(condoWeb);
return shortName;
}
private string getGeneralLandlordUserName(SPWeb condoWeb)
{
string shortName = getCondoShortName(condoWeb);
return string.Format(GENERAL_LANDLORD_NAME_TEMPLATE, shortName);
}
private void info(string msg, params object[] args)
{
this.notify(LogLevel.Info, msg, args);
}
private void warn(string msg, params object[] args)
{
this.notify(LogLevel.Warn, msg, args);
}
private void error(string msg, params object[] args)
{
this.notify(LogLevel.Error, msg, args);
}
private void notify(LogLevel level, string msg, params object[] args)
{
this.notify(level, string.Format(msg, args));
}
private void notify(LogLevel level, string msg)
{
if (this.OnNotify != null)
{
this.OnNotify(this, new LogEventArgs { LogLevel = level, Message = msg });
}
}
}
}