264 lines
9.4 KiB
C#
264 lines
9.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Xml;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Deployment;
|
|
using Microsoft.SharePoint.Navigation;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Publishing.Navigation;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.AddDomusPagev2
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private readonly Guid SITE_FEATURE_ID = new Guid("2e117d47-2815-4a48-9efe-a64ee3c3517f");
|
|
private readonly Guid WEB_FEATURE_ID = new Guid("40bbdc02-73b2-43c0-b06a-37851ef2ec17");
|
|
|
|
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())
|
|
{
|
|
this.ensureSiteFeatureActivated(site);
|
|
|
|
this.deletePage(web, "Tilintarkastus.aspx");
|
|
this.deletePage(web, "Kulutuslukemat.aspx");
|
|
|
|
web.Features.Add(WEB_FEATURE_ID, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void deletePage(SPWeb web, string pageName)
|
|
{
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
var item = pweb.PagesList.Items.Cast<SPListItem>().FirstOrDefault(
|
|
i => { return (i.File != null && string.Compare(i.File.Name, pageName, true) == 0); });
|
|
if (item != null)
|
|
{
|
|
item.Delete();
|
|
}
|
|
}
|
|
|
|
private void ensureSiteFeatureActivated(SPSite site)
|
|
{
|
|
if (!site.Features.Cast<SPFeature>().Any(f => f.DefinitionId == SITE_FEATURE_ID))
|
|
{
|
|
site.Features.Add(SITE_FEATURE_ID, false);
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class SecurityHelper
|
|
{
|
|
public static void AddUserToGroup(SPWeb web, string loginName, string email, string name, string groupName)
|
|
{
|
|
// don't need to add user in group second time
|
|
if (IsUserInGroup(web, loginName, groupName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!containsUser(web, loginName))
|
|
{
|
|
web.EnsureUser(loginName);
|
|
}
|
|
var group = web.SiteGroups[groupName];
|
|
group.AddUser(loginName, email, name, string.Empty);
|
|
}
|
|
|
|
public static void AddUserToGroup(SPWeb web, SPUser user, SPGroup group)
|
|
{
|
|
// don't need to add user in group second time
|
|
if (IsUserInGroup(web, user.LoginName, group.Name))
|
|
{
|
|
return;
|
|
}
|
|
|
|
group.AddUser(user);
|
|
}
|
|
|
|
public static bool IsUserInGroup(SPWeb web, string loginName, string groupName)
|
|
{
|
|
var group = web.SiteGroups[groupName];
|
|
return group.Users.Cast<SPUser>().Any(u => string.Compare(u.LoginName, loginName, true) == 0);
|
|
}
|
|
|
|
public static void CreateUser(SPWeb web, string loginName, string email, string name)
|
|
{
|
|
if (!containsUser(web, loginName))
|
|
{
|
|
try
|
|
{
|
|
var user = web.EnsureUser(loginName);
|
|
user.Email = email;
|
|
user.Update();
|
|
}
|
|
catch (SPException)
|
|
{
|
|
// EnsureUser can fail on one of WFEs because of some reason.
|
|
// To avoid this error add user manually
|
|
//string name = getUserName(loginName);
|
|
if (!string.IsNullOrEmpty(name))
|
|
{
|
|
web.AllUsers.Add(loginName, email, name, "");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static bool containsUser(SPWeb web, string loginName)
|
|
{
|
|
return web.AllUsers.Cast<SPUser>().Any(u => string.Compare(u.LoginName, loginName, true) == 0);
|
|
}
|
|
|
|
// private static string getUserName(string loginName)
|
|
// {
|
|
// if (string.IsNullOrEmpty(loginName))
|
|
// {
|
|
// return string.Empty;
|
|
// }
|
|
// if (loginName.Contains("\\"))
|
|
// {
|
|
// return string.Empty;
|
|
// }
|
|
// string name = loginName.Substring(loginName.LastIndexOf("\\"));
|
|
// return name;
|
|
// }
|
|
|
|
public static SPGroup EnsureSiteGroup(SPWeb web, string groupName)
|
|
{
|
|
if (!IsGroupExist(web, groupName))
|
|
{
|
|
web.SiteGroups.Add(groupName, web.SiteAdministrators[0], null, string.Empty);
|
|
}
|
|
|
|
return web.SiteGroups[groupName];
|
|
}
|
|
|
|
public static SPGroup GetSiteGroup(SPWeb web, string groupName)
|
|
{
|
|
return web.SiteGroups[groupName];
|
|
}
|
|
|
|
public static bool IsGroupExist(SPWeb web, string groupName)
|
|
{
|
|
return web.SiteGroups.Cast<SPGroup>().Any(g => string.Compare(g.Name, groupName, true) == 0);
|
|
}
|
|
|
|
public static void AssignGroupRoleToSecurableObject(SPWeb web, ISecurableObject securableObject,
|
|
SPRoleType roleType, SPGroup group)
|
|
{
|
|
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
|
|
AssignGroupRoleToSecurableObject(web, securableObject, roleDefinition, group, false);
|
|
}
|
|
|
|
public static void AssignGroupRoleToSecurableObject(SPWeb web, ISecurableObject securableObject,
|
|
SPRoleType roleType, SPGroup group, bool copyRoleAssignment)
|
|
{
|
|
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
|
|
AssignGroupRoleToSecurableObject(web, securableObject, roleDefinition, group, copyRoleAssignment);
|
|
}
|
|
|
|
public static void AssignGroupRoleToSecurableObject(SPWeb web, ISecurableObject securableObject,
|
|
SPRoleDefinition roleDefinition, SPGroup group, bool copyRoleAssignment)
|
|
{
|
|
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
|
|
AssignRoleToSecurableObject(web, securableObject, roleDefinition, roleAssignment, copyRoleAssignment);
|
|
}
|
|
|
|
public static void AssignGroupRoleToSecurableObject(SPWeb web, ISecurableObject securableObject,
|
|
SPRoleDefinition roleDefinition, SPGroup group)
|
|
{
|
|
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
|
|
AssignRoleToSecurableObject(web, securableObject, roleDefinition, roleAssignment, true);
|
|
}
|
|
|
|
public static void AssignUserRoleToSecurableObject(SPWeb web, ISecurableObject securableObject,
|
|
SPRoleType roleType, SPUser user)
|
|
{
|
|
SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
|
|
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
|
|
AssignRoleToSecurableObject(web, securableObject, roleDefinition, roleAssignment, true);
|
|
}
|
|
|
|
public static void AssignRoleToSecurableObject(SPWeb web, ISecurableObject securableObject, SPRoleDefinition roleDefinition, SPRoleAssignment roleAssignment,
|
|
bool copyRoleAssignment)
|
|
{
|
|
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
|
|
if (!securableObject.HasUniqueRoleAssignments)
|
|
{
|
|
securableObject.BreakRoleInheritance(copyRoleAssignment);
|
|
// 2013-01-22 apetuhov: BreakRoleInheritance causes reset of AllowUnsafeUpdates to false.
|
|
// See http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/
|
|
if (web != null && !web.AllowUnsafeUpdates)
|
|
{
|
|
web.AllowUnsafeUpdates = true;
|
|
}
|
|
if (securableObject is SPWeb)
|
|
{
|
|
var secureWeb = securableObject as SPWeb;
|
|
if (!secureWeb.AllowUnsafeUpdates)
|
|
{
|
|
secureWeb.AllowUnsafeUpdates = true;
|
|
}
|
|
}
|
|
}
|
|
securableObject.RoleAssignments.Add(roleAssignment);
|
|
}
|
|
}
|
|
}
|