241 lines
8.7 KiB
C#
241 lines
8.7 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Microsoft.SharePoint;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.IDP.AddFlatsArtifacts
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private readonly Guid WEB_FEATURE1_ID = new Guid("dccf4c83-01cf-4e57-ab5a-9fd52efbd814");
|
|
private readonly Guid WEB_FEATURE2_ID = new Guid("3f4b7dbe-c9a8-4b69-8245-b5d644df92e7");
|
|
|
|
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())
|
|
{
|
|
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
this.ensureFeatureActivated(web, WEB_FEATURE1_ID);
|
|
this.ensureFeatureActivated(web, WEB_FEATURE2_ID);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ensureFeatureActivated(SPWeb web, Guid featureId)
|
|
{
|
|
web.Features.Add(featureId, true);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|