pasword expiration scheduler + email tempaltes fixes + accesst oken stored procedures added
This commit is contained in:
parent
aa59d180e2
commit
e320b4c79e
20 changed files with 530 additions and 193 deletions
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer.Extensions
|
||||
{
|
||||
public static class UriExtensions
|
||||
{
|
||||
public static Uri Append(this Uri uri, params string[] paths)
|
||||
{
|
||||
return new Uri(paths.Aggregate(uri.AbsoluteUri, (current, path) => string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/'))));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ using System.Configuration;
|
|||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Text.RegularExpressions;
|
||||
using WebsitePanel.EnterpriseServer.Base.HostedSolution;
|
||||
using WebsitePanel.Providers.HostedSolution;
|
||||
using Microsoft.ApplicationBlocks.Data;
|
||||
using System.Collections.Generic;
|
||||
|
@ -3189,6 +3190,52 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
#region Organizations
|
||||
|
||||
public static int AddAccessToken(AccessToken token)
|
||||
{
|
||||
return AddAccessToken(token.AccessTokenGuid, token.AccountId, token.ItemId, token.ExpirationDate, token.Type);
|
||||
}
|
||||
|
||||
public static int AddAccessToken(Guid accessToken, int accountId, int itemId, DateTime expirationDate, AccessTokenTypes type)
|
||||
{
|
||||
SqlParameter prmId = new SqlParameter("@TokenID", SqlDbType.Int);
|
||||
prmId.Direction = ParameterDirection.Output;
|
||||
|
||||
SqlHelper.ExecuteNonQuery(
|
||||
ConnectionString,
|
||||
CommandType.StoredProcedure,
|
||||
"AddAccessToken",
|
||||
prmId,
|
||||
new SqlParameter("@AccessToken", accessToken),
|
||||
new SqlParameter("@ExpirationDate", expirationDate),
|
||||
new SqlParameter("@AccountID", accountId),
|
||||
new SqlParameter("@ItemId", itemId),
|
||||
new SqlParameter("@TokenType", (int)type)
|
||||
);
|
||||
|
||||
// read identity
|
||||
return Convert.ToInt32(prmId.Value);
|
||||
}
|
||||
|
||||
public static void DeleteExpiredAccessTokens()
|
||||
{
|
||||
SqlHelper.ExecuteNonQuery(
|
||||
ConnectionString,
|
||||
CommandType.StoredProcedure,
|
||||
"DeleteExpiredAccessTokenTokens"
|
||||
);
|
||||
}
|
||||
|
||||
public static IDataReader GetAccessTokenByAccessToken(Guid accessToken, AccessTokenTypes type)
|
||||
{
|
||||
return SqlHelper.ExecuteReader(
|
||||
ConnectionString,
|
||||
CommandType.StoredProcedure,
|
||||
"GetAccessTokenByAccessToken",
|
||||
new SqlParameter("@AccessToken", accessToken),
|
||||
new SqlParameter("@TokenType", type)
|
||||
);
|
||||
}
|
||||
|
||||
public static int AddOrganizationDeletedUser(int accountId, int originAT, string storagePath, string folderName, string fileName, DateTime expirationDate)
|
||||
{
|
||||
SqlParameter outParam = new SqlParameter("@ID", SqlDbType.Int);
|
||||
|
|
|
@ -35,6 +35,7 @@ using System.Net.Mail;
|
|||
using System.Text;
|
||||
using WebsitePanel.EnterpriseServer.Code.HostedSolution;
|
||||
using WebsitePanel.EnterpriseServer.Code.SharePoint;
|
||||
using WebsitePanel.EnterpriseServer.Extensions;
|
||||
using WebsitePanel.Providers;
|
||||
using WebsitePanel.Providers.HostedSolution;
|
||||
using WebsitePanel.Providers.ResultObjects;
|
||||
|
@ -1520,38 +1521,130 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return expiredUsersDb;
|
||||
}
|
||||
|
||||
public static void SendResetUserPasswordEmail(UserInfo owner, OrganizationUser user, string mailTo, string logoUrl)
|
||||
/// <summary>
|
||||
/// Send reset user password email
|
||||
/// </summary>
|
||||
/// <param name="itemId">Organization Id</param>
|
||||
/// <param name="accountId">User Id</param>
|
||||
/// <param name="reason">Reason why reset email is sent.</param>
|
||||
/// <param name="mailTo">Optional, if null accountID user PrimaryEmailAddress will be used</param>
|
||||
public static void SendResetUserPasswordEmail(int itemId, int accountId, string reason, string mailTo = null)
|
||||
{
|
||||
UserSettings settings = UserController.GetUserSettings(owner.UserId, UserSettings.USER_PASSWORD_EXPIRATION_LETTER);
|
||||
// load organization
|
||||
Organization org = GetOrganization(itemId);
|
||||
|
||||
if (string.IsNullOrEmpty(logoUrl))
|
||||
if (org == null)
|
||||
{
|
||||
logoUrl = settings["LogoUrl"];
|
||||
throw new Exception(string.Format("Organization not found (ItemId = {0})", itemId));
|
||||
}
|
||||
|
||||
string from = settings["From"];
|
||||
UserInfo owner = PackageController.GetPackageOwner(org.PackageId);
|
||||
OrganizationUser user = OrganizationController.GetAccount(itemId, accountId);
|
||||
|
||||
string subject = settings["Subject"];
|
||||
string body = owner.HtmlMail ? settings["HtmlBody"] : settings["TextBody"];
|
||||
bool isHtml = owner.HtmlMail;
|
||||
|
||||
MailPriority priority = MailPriority.Normal;
|
||||
|
||||
if (!String.IsNullOrEmpty(settings["Priority"]))
|
||||
if (string.IsNullOrEmpty(mailTo))
|
||||
{
|
||||
priority = (MailPriority)Enum.Parse(typeof(MailPriority), settings["Priority"], true);
|
||||
mailTo = user.PrimaryEmailAddress;
|
||||
}
|
||||
|
||||
Hashtable items = new Hashtable();
|
||||
SendResetUserPasswordEmail(owner, user, mailTo, reason, string.Empty);
|
||||
}
|
||||
|
||||
items["user"] = user;
|
||||
items["logoUrl"] = logoUrl;
|
||||
items["passwordResetLink"] = "reset link";
|
||||
public static void SendResetUserPasswordEmail(UserInfo owner, OrganizationUser user, string reason, string mailTo, string logoUrl)
|
||||
{
|
||||
UserSettings settings = UserController.GetUserSettings(owner.UserId,
|
||||
UserSettings.USER_PASSWORD_EXPIRATION_LETTER);
|
||||
|
||||
body = PackageController.EvaluateTemplate(body, items);
|
||||
TaskManager.StartTask("ORGANIZATION", "SEND_PASSWORD_RESET_EMAIL", user.ItemId);
|
||||
|
||||
// send mail message
|
||||
//MailHelper.SendMessage(from, mailTo, string.Empty, subject, body, priority, isHtml);
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(logoUrl))
|
||||
{
|
||||
logoUrl = settings["LogoUrl"];
|
||||
}
|
||||
|
||||
string from = settings["From"];
|
||||
|
||||
string subject = settings["Subject"];
|
||||
string body = owner.HtmlMail ? settings["HtmlBody"] : settings["TextBody"];
|
||||
bool isHtml = owner.HtmlMail;
|
||||
|
||||
MailPriority priority = MailPriority.Normal;
|
||||
|
||||
if (!String.IsNullOrEmpty(settings["Priority"]))
|
||||
{
|
||||
priority = (MailPriority) Enum.Parse(typeof (MailPriority), settings["Priority"], true);
|
||||
}
|
||||
|
||||
Hashtable items = new Hashtable();
|
||||
|
||||
items["user"] = user;
|
||||
items["logoUrl"] = logoUrl;
|
||||
items["passwordResetLink"] = GenerateUserPasswordResetLink(user.ItemId, user.AccountId);
|
||||
|
||||
body = PackageController.EvaluateTemplate(body, items);
|
||||
|
||||
TaskManager.Write("Organization ID : " + user.ItemId);
|
||||
TaskManager.Write("Account : " + user.DisplayName);
|
||||
TaskManager.Write("Reason : " + reason);
|
||||
TaskManager.Write("MailTo : " + mailTo);
|
||||
|
||||
// send mail message
|
||||
//MailHelper.SendMessage(from, mailTo, string.Empty, subject, body, priority, isHtml);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw TaskManager.WriteError(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
TaskManager.CompleteTask();
|
||||
}
|
||||
}
|
||||
|
||||
public static AccessToken GetAccessToken(Guid accessToken, AccessTokenTypes type)
|
||||
{
|
||||
return ObjectUtils.FillObjectFromDataReader<AccessToken>(DataProvider.GetAccessTokenByAccessToken(accessToken, type));
|
||||
}
|
||||
|
||||
public static void DeleteAllExpiredTokens()
|
||||
{
|
||||
DataProvider.DeleteExpiredAccessTokens();
|
||||
}
|
||||
|
||||
private static string GenerateUserPasswordResetLink(int itemId, int accountId)
|
||||
{
|
||||
string passwordResetUrlFormat = "account/password-reset";
|
||||
|
||||
var settings = SystemController.GetSystemSettings(SystemSettings.WEBDAV_PORTAL_SETTINGS);
|
||||
|
||||
if (settings == null)
|
||||
{
|
||||
throw new Exception("Webdav portal system settings are not set");
|
||||
}
|
||||
|
||||
var webdavPortalUrl = new Uri(settings["WebdavPortalUrl"]);
|
||||
|
||||
var token = CreateAccessToken(itemId, accountId, AccessTokenTypes.PasswrodReset);
|
||||
|
||||
return webdavPortalUrl.Append(passwordResetUrlFormat)
|
||||
.Append(token.AccessTokenGuid.ToString("n")).ToString();
|
||||
}
|
||||
|
||||
private static AccessToken CreateAccessToken(int itemId, int accountId, AccessTokenTypes type)
|
||||
{
|
||||
var token = new AccessToken
|
||||
{
|
||||
AccessTokenGuid = Guid.NewGuid(),
|
||||
ItemId = itemId,
|
||||
AccountId = accountId,
|
||||
Type = type,
|
||||
ExpirationDate = DateTime.Now.AddHours(12)
|
||||
};
|
||||
|
||||
token.Id = DataProvider.AddAccessToken(token);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
private static bool EmailAddressExists(string emailAddress)
|
||||
|
@ -2288,7 +2381,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
#endregion
|
||||
|
||||
// place log record
|
||||
TaskManager.StartTask("ORGANIZATION", "GET_USER_GENERAL", itemId);
|
||||
//TaskManager.StartTask("ORGANIZATION", "GET_USER_GENERAL", itemId);
|
||||
|
||||
OrganizationUser account = null;
|
||||
Organization org = null;
|
||||
|
@ -2331,7 +2424,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
catch { }
|
||||
finally
|
||||
{
|
||||
TaskManager.CompleteTask();
|
||||
//TaskManager.CompleteTask();
|
||||
}
|
||||
|
||||
return (account);
|
||||
|
|
|
@ -9,8 +9,6 @@ namespace WebsitePanel.EnterpriseServer
|
|||
{
|
||||
public class UserPasswordExpirationNotificationTask : SchedulerTask
|
||||
{
|
||||
private static readonly string TaskId = "SCHEDULE_TASK_DOMAIN_EXPIRATION";
|
||||
|
||||
// Input parameters:
|
||||
private static readonly string DaysBeforeNotify = "DAYS_BEFORE_EXPIRATION";
|
||||
|
||||
|
@ -27,6 +25,8 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return;
|
||||
}
|
||||
|
||||
OrganizationController.DeleteAllExpiredTokens();
|
||||
|
||||
var owner = UserController.GetUser(topTask.EffectiveUserId);
|
||||
|
||||
var packages = PackageController.GetMyPackages(topTask.EffectiveUserId);
|
||||
|
@ -41,15 +41,15 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
foreach (var user in usersWithExpiredPasswords)
|
||||
{
|
||||
user.ItemId = organization.Id;
|
||||
|
||||
if (string.IsNullOrEmpty(user.PrimaryEmailAddress))
|
||||
{
|
||||
TaskManager.WriteWarning(string.Format("Unable to send email to {0} user (organization: {1}), user primary email address is not set.", user.DisplayName, organization.OrganizationId));
|
||||
continue;
|
||||
}
|
||||
|
||||
TaskManager.Write(string.Format("Email sent to {0} user (organization: {1}).", user.DisplayName, organization.OrganizationId));
|
||||
|
||||
OrganizationController.SendResetUserPasswordEmail(owner, user, user.PrimaryEmailAddress, string.Empty);
|
||||
OrganizationController.SendResetUserPasswordEmail(owner, user, "Scheduler Password Expiration Notification", user.PrimaryEmailAddress, string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,6 +75,7 @@
|
|||
<Compile Include="Common\CryptoUtils.cs" />
|
||||
<Compile Include="Common\EnterpriseServerIdentity.cs" />
|
||||
<Compile Include="Common\EnterpriseServerPrincipal.cs" />
|
||||
<Compile Include="Common\Extensions\UriExtensions.cs" />
|
||||
<Compile Include="Common\FileUtils.cs" />
|
||||
<Compile Include="Common\Int128.cs" />
|
||||
<Compile Include="Common\IPAddress.cs" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue