protected static string GenerateSecureString(int length, string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+{}:'/?-") { int outOfRange = byte.MaxValue + 1 - (byte.MaxValue + 1) % alphabet.Length; return(string.Concat( Enumerable .Repeat(0, int.MaxValue) .Select(e => ApiInterfaceUser.RandomByte()) .Where(randomByte => randomByte < outOfRange) .Take(length) .Select(randomByte => alphabet[randomByte % alphabet.Length]) )); }
public virtual object CreateInstance(CSEntryChange csentry) { if (csentry.ObjectModificationType == ObjectModificationType.Add) { if (csentry.ObjectType == SchemaConstants.User) { return(new User { Password = ApiInterfaceUser.GenerateSecureString(60), PrimaryEmail = csentry.DN }); } else { User u = new User { Password = ApiInterfaceUser.GenerateSecureString(60), PrimaryEmail = csentry.DN, CustomSchemas = new Dictionary <string, IDictionary <string, object> > { { SchemaConstants.CustomGoogleAppsSchemaName, new Dictionary <string, object>() } } }; u.CustomSchemas[SchemaConstants.CustomGoogleAppsSchemaName].Add(SchemaConstants.CustomSchemaObjectType, csentry.ObjectType); return(u); } } else { return(new User { Id = csentry.GetAnchorValueOrDefault <string>("id"), PrimaryEmail = csentry.DN }); } }
public IList <AttributeChange> ApplyChanges(CSEntryChange csentry, SchemaType type, ref object target, bool patch = false) { bool hasChanged = false; List <AttributeChange> changes = new List <AttributeChange>(); if (!(target is User user)) { throw new InvalidOperationException(); } if (ApiInterfaceUser.SetDNValue(csentry, user)) { hasChanged = true; } foreach (IAttributeAdapter typeDef in this.SchemaType.AttributeAdapters.Where(t => t.Api == this.Api)) { if (typeDef.UpdateField(csentry, target)) { hasChanged = true; } } if (hasChanged) { Trace.WriteLine($"Object {csentry.DN} has one or more changes to commit"); User result; if (csentry.ObjectModificationType == ObjectModificationType.Add) { result = this.config.UsersService.Add(user); target = result; } else if (csentry.ObjectModificationType == ObjectModificationType.Replace || csentry.ObjectModificationType == ObjectModificationType.Update) { string id = csentry.GetAnchorValueOrDefault <string>("id"); if (patch) { result = this.config.UsersService.Patch(user, id); } else { result = this.config.UsersService.Update(user, id); } target = result; } else { throw new InvalidOperationException(); } changes.AddRange(this.GetLocalChanges(csentry.DN, csentry.ObjectModificationType, type, result)); } else { Trace.WriteLine($"Object {csentry.DN} has no changes to commit"); } foreach (IApiInterface i in this.InternalInterfaces) { foreach (AttributeChange c in i.ApplyChanges(csentry, type, ref target, patch)) { changes.Add(c); } } this.AddMissingDeletes(changes, csentry); return(changes); }