2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2020-09-17 09:42:55 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-04-12 15:11:07 +02:00
|
|
|
|
using NPoco;
|
2016-05-18 23:34:56 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
using Umbraco.Core.Models.Entities;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using Umbraco.Core.Models.Identity;
|
2017-12-28 09:06:33 +01:00
|
|
|
|
using Umbraco.Core.Persistence.Dtos;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using Umbraco.Core.Persistence.Factories;
|
|
|
|
|
|
using Umbraco.Core.Persistence.Querying;
|
2017-12-12 15:04:13 +01:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2017-12-07 16:45:25 +01:00
|
|
|
|
namespace Umbraco.Core.Persistence.Repositories.Implement
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2016-04-12 15:11:07 +02:00
|
|
|
|
internal class ExternalLoginRepository : NPocoRepositoryBase<int, IIdentityUserLogin>, IExternalLoginRepository
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
public ExternalLoginRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger<ExternalLoginRepository> logger)
|
2017-12-14 17:04:44 +01:00
|
|
|
|
: base(scopeAccessor, cache, logger)
|
2017-12-12 15:04:13 +01:00
|
|
|
|
{ }
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
|
|
|
|
|
public void DeleteUserLogins(int memberId)
|
|
|
|
|
|
{
|
2016-05-30 19:47:49 +02:00
|
|
|
|
Database.Execute("DELETE FROM ExternalLogins WHERE UserId=@userId", new { userId = memberId });
|
2015-02-09 17:37:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-12 14:39:27 +11:00
|
|
|
|
public void SaveUserLogins(int memberId, IEnumerable<IUserLoginInfo> logins)
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2016-05-30 19:47:49 +02:00
|
|
|
|
//clear out logins for member
|
|
|
|
|
|
Database.Execute("DELETE FROM umbracoExternalLogin WHERE userId=@userId", new { userId = memberId });
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2016-05-30 19:47:49 +02:00
|
|
|
|
//add them all
|
|
|
|
|
|
foreach (var l in logins)
|
|
|
|
|
|
{
|
|
|
|
|
|
Database.Insert(new ExternalLoginDto
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2016-05-30 19:47:49 +02:00
|
|
|
|
LoginProvider = l.LoginProvider,
|
|
|
|
|
|
ProviderKey = l.ProviderKey,
|
|
|
|
|
|
UserId = memberId,
|
|
|
|
|
|
CreateDate = DateTime.Now
|
|
|
|
|
|
});
|
2015-02-09 17:37:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IIdentityUserLogin PerformGet(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sql = GetBaseQuery(false);
|
2019-04-02 23:00:08 -04:00
|
|
|
|
sql.Where(GetBaseWhereClause(), new { id = id });
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2016-08-02 12:11:35 +02:00
|
|
|
|
var dto = Database.Fetch<ExternalLoginDto>(SqlSyntax.SelectTop(sql, 1)).FirstOrDefault();
|
|
|
|
|
|
if (dto == null)
|
2015-02-09 17:37:21 +11:00
|
|
|
|
return null;
|
|
|
|
|
|
|
2018-06-29 11:48:47 +01:00
|
|
|
|
var entity = ExternalLoginFactory.BuildEntity(dto);
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
|
// reset dirty initial properties (U4-1946)
|
2015-11-10 16:14:03 +01:00
|
|
|
|
entity.ResetDirtyProperties(false);
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<IIdentityUserLogin> PerformGetAll(params int[] ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ids.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return PerformGetAllOnIds(ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var sql = GetBaseQuery(false);
|
|
|
|
|
|
|
|
|
|
|
|
return ConvertFromDtos(Database.Fetch<ExternalLoginDto>(sql))
|
|
|
|
|
|
.ToArray();// we don't want to re-iterate again!
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IIdentityUserLogin> PerformGetAllOnIds(params int[] ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ids.Any() == false) yield break;
|
|
|
|
|
|
foreach (var id in ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return Get(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IIdentityUserLogin> ConvertFromDtos(IEnumerable<ExternalLoginDto> dtos)
|
|
|
|
|
|
{
|
2018-06-29 11:48:47 +01:00
|
|
|
|
foreach (var entity in dtos.Select(ExternalLoginFactory.BuildEntity))
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2017-11-10 11:27:12 +01:00
|
|
|
|
// reset dirty initial properties (U4-1946)
|
2018-01-10 12:48:51 +01:00
|
|
|
|
((BeingDirtyBase)entity).ResetDirtyProperties(false);
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
|
|
|
|
|
yield return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<IIdentityUserLogin> PerformGetByQuery(IQuery<IIdentityUserLogin> query)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sqlClause = GetBaseQuery(false);
|
|
|
|
|
|
var translator = new SqlTranslator<IIdentityUserLogin>(sqlClause, query);
|
|
|
|
|
|
var sql = translator.Translate();
|
|
|
|
|
|
|
|
|
|
|
|
var dtos = Database.Fetch<ExternalLoginDto>(sql);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var dto in dtos)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return Get(dto.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-22 18:48:58 +02:00
|
|
|
|
protected override Sql<ISqlContext> GetBaseQuery(bool isCount)
|
2015-02-09 17:37:21 +11:00
|
|
|
|
{
|
2016-04-12 15:11:07 +02:00
|
|
|
|
var sql = Sql();
|
2015-02-09 17:37:21 +11:00
|
|
|
|
if (isCount)
|
2016-04-12 15:11:07 +02:00
|
|
|
|
sql.SelectCount();
|
2015-02-09 17:37:21 +11:00
|
|
|
|
else
|
2016-04-12 15:11:07 +02:00
|
|
|
|
sql.SelectAll();
|
|
|
|
|
|
sql.From<ExternalLoginDto>();
|
2015-02-09 17:37:21 +11:00
|
|
|
|
return sql;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override string GetBaseWhereClause()
|
|
|
|
|
|
{
|
2017-11-10 11:27:12 +01:00
|
|
|
|
return "umbracoExternalLogin.id = @id";
|
2015-02-09 17:37:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<string> GetDeleteClauses()
|
|
|
|
|
|
{
|
|
|
|
|
|
var list = new List<string>
|
|
|
|
|
|
{
|
2017-11-10 11:27:12 +01:00
|
|
|
|
"DELETE FROM umbracoExternalLogin WHERE id = @id"
|
2015-02-09 17:37:21 +11:00
|
|
|
|
};
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Guid NodeObjectTypeId
|
|
|
|
|
|
{
|
|
|
|
|
|
get { throw new NotImplementedException(); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void PersistNewItem(IIdentityUserLogin entity)
|
|
|
|
|
|
{
|
2019-06-28 09:19:11 +02:00
|
|
|
|
entity.AddingEntity();
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2018-06-29 11:48:47 +01:00
|
|
|
|
var dto = ExternalLoginFactory.BuildDto(entity);
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
|
|
|
|
|
var id = Convert.ToInt32(Database.Insert(dto));
|
|
|
|
|
|
entity.Id = id;
|
|
|
|
|
|
|
|
|
|
|
|
entity.ResetDirtyProperties();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void PersistUpdatedItem(IIdentityUserLogin entity)
|
|
|
|
|
|
{
|
2019-06-28 09:19:11 +02:00
|
|
|
|
entity.UpdatingEntity();
|
2019-12-17 16:30:26 +01:00
|
|
|
|
|
2018-06-29 11:48:47 +01:00
|
|
|
|
var dto = ExternalLoginFactory.BuildDto(entity);
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
Database.Update(dto);
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
|
|
|
|
|
entity.ResetDirtyProperties();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|