Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
77 lines (63 sloc) 2.12 KB
/*
* Magic, Copyright(c) Thomas Hansen 2019 - thomas@gaiasoul.com
* Licensed as Affero GPL unless an explicitly proprietary license has been obtained.
*/
using System;
using System.Linq;
using log4net;
using Ninject;
using NHibernate;
using Newtonsoft.Json.Linq;
using bc = BCrypt.Net;
using magic.auth.model;
using magic.auth.contracts;
using magic.common.services;
using magic.signals.contracts;
namespace magic.auth.services
{
public class UserService : CrudService<User>, IUserService
{
readonly ISignaler _signaler;
public UserService([Named("default")] ISession session, ISignaler signaler)
: base(session, LogManager.GetLogger(typeof(UserService)))
{
_signaler = signaler ?? throw new ArgumentNullException(nameof(signaler));
}
#region [ -- Overrides -- ]
public override Guid Save(User model)
{
if (string.IsNullOrEmpty(model.Password))
{
if (model.Id == Guid.Empty)
throw new ArgumentNullException($"When initially saving your User, you have to provide a password");
var oldModel = Session.Query<User>()
.Where(x => x.Id == model.Id)
.FirstOrDefault();
if (oldModel == null)
throw new ArgumentOutOfRangeException($"User with id of '{model.Id}' does not exist");
model.Password = oldModel.Password;
}
else
{
model.Password = bc.BCrypt.HashPassword(model.Password);
}
return base.Save(model);
}
public override void Delete(Guid id)
{
using (var transaction = Session.BeginTransaction())
{
_signaler.Signal("user.deleted", new JObject
{
["id"] = id,
});
var user = Session.Load<User>(id);
user.Roles.Clear();
Session.Flush();
base.Delete(id);
transaction.Commit();
}
}
#endregion
}
}
You can’t perform that action at this time.