-
I'm trying to have a client - server setup where the server is authorising clients and assigning userIds. This means that I need to add a userId to all rows coming into the server and filter the rows going out on that userId. The client itself has no knowledge of this userId. Syncing the columns is easily done by including all columns except the userId, but how do I add the userId to the rows incoming for the server (from the client)? As for the part where the server only sends the filtered data to the client, this probably can be done with filters, but the parameter value needs to come from the client, which has no knowledge of the userId (since that is a server thing) Any pointers? Thank you for the amazing library! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 23 replies
-
I had the similar task. Server sizeOn server side when configuring columns those colums were excluded from sync. var types = typeof(Entity.SyncModelBase).Assembly.GetTypes()
.Where(x => x.IsSubclassOf(typeof(Entity.SyncModelBase)) && x.GetTypeInfo().GetCustomAttribute<Entity.TableAttribute>() != null)
.ToArray();
var typesInfo = types.ToDictionary(
x => x.GetTypeInfo().GetCustomAttribute<Entity.TableAttribute>()?.Name,
x => x);
var syncSetup = new SyncSetup(typesInfo.Keys);
foreach (var info in typesInfo)
{
var columns = info.Value.GetProperties()
.Where(x => x.GetCustomAttribute<Entity.IgnoreAttribute>() == null)
.Select(x => x.GetCustomAttribute<Entity.ColumnAttribute>()?.Name)
.Where(x => x != null);
syncSetup.Tables[info.Key].Columns.AddRange(columns);
//Creating filter by UserId
var filter = new SetupFilter(info.Key);
filter.AddParameter("userId", System.Data.DbType.String);
filter.AddWhere("UserId", info.Key, "userId");
syncSetup.Filters.Add(filter);
} Also here you can see that I'm filtering all data according to the Client sideIn general, table configuration is the same, except the thing that you need to fill correct var types = typeof(Model.ISyncModel).Assembly.GetTypes()
.Where(x => x.IsSubclassOf(typeof(Model.SyncModelBase)) && x.GetTypeInfo().GetCustomAttribute<SQLite.Attributes.TableAttribute>() != null)
.ToArray();
var typesInfo = types.ToDictionary(
x => x.GetTypeInfo().GetCustomAttribute<SQLite.Attributes.TableAttribute>()?.Name,
x => x);
var syncSetup = new SyncSetup(typesInfo.Keys);
foreach (var info in typesInfo)
{
var columns = info.Value.GetProperties()
.Where(x => x.GetCustomAttribute<SQLite.Attributes.IgnoreAttribute>() == null)
.Select(x => x.GetCustomAttribute<SQLite.Attributes.ColumnAttribute>()?.Name)
.Where(x => x != null)
.ToArray();
syncSetup.Tables[info.Key].Columns.AddRange(columns);
}
// Creating an agent that will handle all the process
var agent = new SyncAgent(clientProvider, serverOrchestrator, new SyncOptions() { UseVerboseErrors = true }, syncSetup);
agent.Parameters.Add("userId", userId); Important thise is an additional data converter when you're creating server orchestrator. My idea of the trick: fill var serverOrchestrator = new WebClientOrchestrator("https://localhost:6001/api/sync", null, new AuthUserIdConverter(userId));
...
public sealed class AuthUserIdConverter : Dotmim.Sync.Serialization.IConverter
{
private string userId;
public AuthUserIdConverter(string userId)
{
this.userId = userId;
}
public string Key { get; } = "UserId";
public void AfterDeserialized(SyncRow row)
{
row["UserId"] = null;
}
public void BeforeSerialize(SyncRow row)
{
row["UserId"] = userId;
}
} |
Beta Was this translation helpful? Give feedback.
-
Since it's not an issue, I'm converting this Q&A to a discussion 👍 Regarding your particular case, I guess using an |
Beta Was this translation helpful? Give feedback.
-
I think you can use the AfterDeserialized event on the server, you have access to the incoming rows and in that event you can change the id feomy the client. So you could send blanks on the client and change it to the authorized user id (or whatever is you need) in that event, and then on the way out the server you can add or remove any user id values too. I think that's how it could work?--Sent from my Android phone with GMX Mail. Please excuse my brevity.On 21/01/2021, 12:40 Tuytje <notifications@github.com> wrote:
I was looking into this as well, but could find a way to do it when it wasn't coming from the client. I have no issue with the id coming from the client since, in my case, it doesn't matter. I will look into confirming the logged in userid with the given user id, to make sure the client didn't tamper with it. Might be a way forward?
—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
Hey guys, please have a look here : #391 (comment) |
Beta Was this translation helpful? Give feedback.
I had the similar task.
Each task got additional columns
UserId
.Server size
On server side when configuring columns those colums were excluded from sync.