Skip to content

Commit

Permalink
fix: replace ObjectId by strings so that insertion during collection …
Browse files Browse the repository at this point in the history
…initialization works
  • Loading branch information
aneojgurhem committed Oct 11, 2024
1 parent 33ea992 commit ba0f910
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ static AuthDataModelMapping()
BsonClassMap.RegisterClassMap<AuthData>(cm =>
{
cm.MapIdProperty(nameof(AuthData.AuthId))
.SetIsRequired(true)
.SetSerializer(IdSerializer.Instance);
.SetIsRequired(true);
cm.MapProperty(nameof(AuthData.UserId))
.SetIsRequired(true)
.SetSerializer(IdSerializer.Instance);
.SetIsRequired(true);
cm.MapProperty(nameof(AuthData.Cn))
.SetIsRequired(true);
cm.MapProperty(nameof(AuthData.Fingerprint))
Expand Down
156 changes: 0 additions & 156 deletions Adaptors/MongoDB/src/Table/DataModel/Auth/AuthMongoUtils.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

using ArmoniK.Core.Common.Auth.Authentication;

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace ArmoniK.Core.Adapters.MongoDB.Table.DataModel.Auth;
Expand All @@ -34,8 +33,8 @@ namespace ArmoniK.Core.Adapters.MongoDB.Table.DataModel.Auth;
/// <param name="UserData">List of users that have the id UserId</param>
[BsonIgnoreExtraElements]
public record AuthDataAfterLookup([property: BsonId]
ObjectId AuthId,
ObjectId UserId,
string AuthId,
string UserId,
string Cn,
string Fingerprint,
UserData[] UserData);
Expand All @@ -48,7 +47,7 @@ public record AuthDataAfterLookup([property: BsonId]
/// <param name="Roles">List of roles of the user</param>
[BsonIgnoreExtraElements]
public record UserDataAfterLookup([property: BsonId]
ObjectId UserId,
string UserId,
string Username,
IEnumerable<RoleData> Roles);

Expand All @@ -60,7 +59,7 @@ public record UserDataAfterLookup([property: BsonId]
/// <param name="Roles">User's roles</param>
/// <param name="Permissions">User's permissions</param>
public record MongoAuthResult([property: BsonId]
ObjectId Id,
string Id,
string Username,
IEnumerable<string> Roles,
IEnumerable<string> Permissions)
Expand All @@ -70,7 +69,7 @@ public record MongoAuthResult([property: BsonId]
/// </summary>
/// <returns>UserAuthenticationResult from this object</returns>
public UserAuthenticationResult ToUserAuthenticationResult()
=> new(IdSerializer.Deserialize(Id),
=> new(Id,
Username,
Roles,
Permissions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ static RoleDataModelMapping()
BsonClassMap.RegisterClassMap<RoleData>(cm =>
{
cm.MapIdProperty(nameof(RoleData.RoleId))
.SetIsRequired(true)
.SetSerializer(IdSerializer.Instance);
.SetIsRequired(true);
cm.MapProperty(nameof(RoleData.RoleName))
.SetIsRequired(true);
cm.MapProperty(nameof(RoleData.Permissions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ static UserDataModelMapping()
BsonClassMap.RegisterClassMap<UserData>(cm =>
{
cm.MapIdProperty(nameof(UserData.UserId))
.SetIsRequired(true)
.SetSerializer(IdSerializer.Instance);
.SetIsRequired(true);
cm.MapProperty(nameof(UserData.Username))
.SetIsRequired(true);
cm.MapProperty(nameof(UserData.Roles))
.SetIgnoreIfDefault(true)
.SetSerializer(IdArraySerializer.Instance)
.SetDefaultValue(Array.Empty<string>());
cm.SetIgnoreExtraElements(true);
cm.MapCreator(model => new UserData(model.UserId,
Expand Down
3 changes: 0 additions & 3 deletions Adaptors/MongoDB/tests/AuthenticationTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using System;

using ArmoniK.Core.Adapters.MongoDB.Table.DataModel.Auth;
using ArmoniK.Core.Common.Auth.Authentication;
using ArmoniK.Core.Common.Tests.TestBase;

Expand All @@ -44,8 +43,6 @@ static AuthenticationTableTest()
BsonClassMap.RegisterClassMap<UserAuthenticationResult>(cm =>
{
cm.MapIdProperty(nameof(UserAuthenticationResult.Id))
.SetSerializer(IdSerializer.Instance)
.SetShouldSerializeMethod(_ => true)
.SetIsRequired(true);
cm.MapProperty(nameof(UserAuthenticationResult.Username))
.SetIsRequired(true);
Expand Down
14 changes: 7 additions & 7 deletions Adaptors/MongoDB/tests/BsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ public void SerializeTaskDataModel()
[Test]
public void SerializeUserDataModel()
{
var udm = new UserData("UserId".ToOidString(),
var udm = new UserData("UserId",
"Username",
new[]
{
"RoleId1".ToOidString(),
"RoleId2".ToOidString(),
"RoleId1",
"RoleId2",
});
var serialized = udm.ToBson();

Expand All @@ -267,7 +267,7 @@ public void SerializeUserDataModel()
[Test]
public void SerializeRoleDataModel()
{
var rdm = new RoleData("RoleId".ToOidString(),
var rdm = new RoleData("RoleId",
"RoleName",
new[]
{
Expand Down Expand Up @@ -295,8 +295,8 @@ public void SerializeRoleDataModel()
[Test]
public void SerializeAuthDataModel()
{
var adm = new AuthData("AuthId".ToOidString(),
"UserId".ToOidString(),
var adm = new AuthData("AuthId",
"UserId",
"CN",
"Fingerprint");
var serialized = adm.ToBson();
Expand All @@ -319,7 +319,7 @@ public void SerializeAuthDataModel()
[Test]
public void SerializeUserAuthenticationResult()
{
var uirm = new UserAuthenticationResult("Id".ToOidString(),
var uirm = new UserAuthenticationResult("Id",
"Username",
new[]
{
Expand Down
29 changes: 15 additions & 14 deletions Common/src/Injection/Options/Database/InitDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -66,34 +67,34 @@ public InitDatabase(InitServices initServices)

Roles = initServices.Authentication.Roles.Select(Role.FromJson)
.OrderBy(role => role.Name)
.Select((role,
i) => new RoleData(i.ToString(),
role.Name,
role.Permissions.ToArray()))
.Select(role => new RoleData(Guid.NewGuid()
.ToString(),
role.Name,
role.Permissions.ToArray()))
.AsICollection();

var roleDic = Roles.ToDictionary(data => data.RoleName,
data => data.RoleId);

Users = initServices.Authentication.Users.Select(User.FromJson)
.OrderBy(user => user.Name)
.Select((user,
i) => new UserData(i.ToString(),
user.Name,
user.Roles.Select(roleName => roleDic[roleName])
.ToArray()))
.Select(user => new UserData(Guid.NewGuid()
.ToString(),
user.Name,
user.Roles.Select(roleName => roleDic[roleName])
.ToArray()))
.AsICollection();

var userDic = Users.ToDictionary(data => data.Username,
data => data.UserId);

Auths = initServices.Authentication.UserCertificates.Select(Certificate.FromJson)
.OrderBy(certificate => (certificate.Fingerprint, certificate.CN))
.Select((certificate,
i) => new AuthData(i.ToString(),
userDic[certificate.User],
certificate.CN,
certificate.Fingerprint))
.Select(certificate => new AuthData(Guid.NewGuid()
.ToString(),
userDic[certificate.User],
certificate.CN,
certificate.Fingerprint))
.AsICollection();

Partitions = initServices.Partitioning.Partitions.Select(Partition.FromJson)
Expand Down
Loading

0 comments on commit ba0f910

Please sign in to comment.