Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
ContactChangeModel.cs 7.56 KiB
using Coscine.Database.DataModel;
using Coscine.Database.ReturnObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace Coscine.Database.Models
{
// ------------------------------------------------------------------------------------
// This Class makes use of "GetAllWhere(...Id)" method rather than "GetById(...Id)"
// to make it future proof in case on a later stage more than a single entry
// is allowed inside the ContactChange table (e.g. history of contact change requests).
// To achieve that, remove usage of "DeleteAllDbEntries(...Id)" method.
// The line in quesiton is marked with an appropiate comment.
// ------------------------------------------------------------------------------------
public class ContactChangeModel : DatabaseModel<ContactChange>
{
public bool UserHasEmailsToConfirm(Guid userId)
{
// Perform deletion of all expired entries (older than 24 Hours).
DeleteExpiredDbEntries(userId);
IEnumerable<ContactChange> emailData = GetAllWhere((contactChange) => contactChange.UserId == userId);
// Return True if entries for a user exist inside the database table ContactChange. Else return False.
if (emailData.Count() > 0)
{
return true;
}
else
{
return false;
}
}
public ContactChangeObject NewEmailChangeRequest(Guid userId, string email)
{
if (UserHasEmailsToConfirm(userId))
{
DeleteAllDbEntries(userId); // <--- REMOVE THIS LINE IF YOU WANT TO KEEP HISTORY OF EMAIL CHANGES.
}
ContactChangeObject contactChangeObject = AddDbEntry(userId, email);
// Sending a confirmation email after an addition to the database is handled by the Controller.
return contactChangeObject;
}
public List<ContactChangeObject> GetEmailsForConfirmation(Guid userId)
{
List<ContactChangeObject> contactChangeObjects = new List<ContactChangeObject>();
IEnumerable<ContactChange> emailData = GetAllWhere((contactChange) => contactChange.UserId == userId);
foreach (var entry in emailData)
{
contactChangeObjects.Add(ToObject(entry));
}
return contactChangeObjects;
}
public UserObject ExecuteConfirmation(Guid token)
{
ContactChange emailData = GetWhere((contactChange) => contactChange.ConfirmationToken == token);
if (emailData != null)
{
if (emailData.EditDate != null)
{
// Add 23 Hours, 59 Minutes and 59 Seconds to EditDate, see when the token has to expire and compare with Now.
DateTime expirationDateTime = emailData.EditDate.Value.AddHours(23).AddMinutes(59).AddSeconds(59);
var compareDateTime = DateTime.Compare(expirationDateTime, DateTime.Now);
// Token EXPIRED when expirationDateTime = -1 (Expiration is BEFORE Now)
// Token VALID when expirationDateTime = 0 OR = 1 (Expiration IS Now or AFTER Now)
if (compareDateTime >= 0)