Skip to content
Snippets Groups Projects
Commit e3f2a3e3 authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

fixing collision with contact change ticket (coscine/issues#1360)

parent 9b134b13
No related branches found
No related tags found
1 merge request!119Hotfix/1360 database scaffolding
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
{
public class ContactChangeModel : DatabaseModel<ContactChange>
{
public bool UserHasEmailsToConfirm(Guid userId)
{
IEnumerable<ContactChange> emailData = GetAllWhere((contactChange) => contactChange.UserId == userId);
// Return True if entries for a user exist inside the db Table ContactChange. Else return False.
if (emailData.Count() > 0)
return true;
else
return false;
}
public ContactChangeObject NewEmailChangeRequest(Guid userId, string email)
{
if (UserHasEmailsToConfirm(userId))
DeleteExpiredEntriesFromDatabase(userId);
ContactChangeObject contactChangeObject = AddEntryToDatabase(userId, email);
SendConfirmationEmail(contactChangeObject.UserId, contactChangeObject.NewEmail, contactChangeObject.ConfirmationToken);
return contactChangeObject;
}
public List<ContactChangeObject> GetUserEmailsForConfirmation(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 InitiateEmailConfirmation(Guid userId, Guid token)
{
ContactChange emailData = GetWhere((contactChange) => contactChange.UserId == userId && contactChange.ConfirmationToken == token);
if (emailData != null)
{
if (emailData.EditDate != null)
{
// Add 23 Hours, 59 Minutes and 59 Seconds to EditDate to 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)
{
// VALID
UserModel userModel = new UserModel();
User user = userModel.GetWhere((usr) => usr.Id == userId);
user.EmailAddress = emailData.NewEmail; // Overwrite old Email with New.
userModel.Update(user); // Update Database (User Table).
Delete(emailData); // Delete Entry from Database (ContactChange Table).
UserObject userObject = userModel.CreateReturnObjectFromDatabaseObject(userModel.GetWhere((usr) => usr.Id == userId));
return userObject;
}
else
{
// EXPIRED
throw new Exception("EXPIRED: Token " + token.ToString() + " for User with an ID " + userId.ToString() + " has expired.");
}
}
else
{
// INVALID (null EditDate)
throw new ArgumentNullException("INVALID: Value EditDate is NULL for Token " + token.ToString() + " and User with an ID " + userId.ToString() + ".");
}
}
else
{
// INVALID (token-user combination not in Database)
throw new MissingFieldException("INVALID: The Token " + token.ToString() + " and User with an ID " + userId.ToString() + " combination is not valid. No entry inside the Database.");
}
}
private ContactChangeObject ToObject(ContactChange contactChange)
{
return new ContactChangeObject(
contactChange.RelationId,
contactChange.UserId,
contactChange.NewEmail,
contactChange.EditDate,
contactChange.ConfirmationToken
);
}
private void DeleteExpiredEntriesFromDatabase(Guid userId)
{
IEnumerable<ContactChange> emailData = GetAllWhere((contactChange) => contactChange.UserId == userId);
emailData.All((entry) =>
{
ContactChange contactChange = new ContactChange()
{
RelationId = entry.RelationId,
UserId = entry.UserId,
NewEmail = entry.NewEmail,
EditDate = entry.EditDate,
ConfirmationToken = entry.ConfirmationToken
};
Delete(contactChange);
return true;
});
}
private ContactChangeObject AddEntryToDatabase(Guid userId, string email)
{
// Create new entry inside the Database for an Email Change Request for the specific User with an Id.
ContactChange contactChange = new ContactChange()
{
RelationId = Guid.NewGuid(),
UserId = userId,
NewEmail = email,
EditDate = System.DateTime.Now,
ConfirmationToken = Guid.NewGuid()
};
Insert(contactChange);
// Send a Confirmation Email to User's New Email with a Confirmation Token Link
// TODO
return ToObject(contactChange);
}
private void SendConfirmationEmail(Guid userId, string email, Guid confrimationToken)
{
// TODO: Send an email with a clickable url incl. confirmation token.
// Notify old email about the change.
}
public override Expression<Func<ContactChange, Guid>> GetIdFromObject()
{
return (contactChange) => contactChange.RelationId;
}
public override Microsoft.EntityFrameworkCore.DbSet<ContactChange> GetITableFromDatabase(CoscineDB db)
{
return db.ContactChanges;
}
public override void SetObjectId(ContactChange databaseObject, Guid id)
{
databaseObject.RelationId = id;
}
}
}
using System;
namespace Coscine.Database.ReturnObjects
{
public class ContactChangeObject : IReturnObject
{
public Guid RelationId { get; set; }
public Guid UserId { get; set; }
public string NewEmail { get; set; }
public DateTime? EditDate { get; set; }
public Guid ConfirmationToken { get; set; }
public ContactChangeObject(Guid relationId, Guid userId, string newEmail, DateTime? editDate, Guid confirmationToken)
{
RelationId = relationId;
UserId = userId;
NewEmail = newEmail;
EditDate = editDate;
ConfirmationToken = confirmationToken;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment