public static void UpdateUser(string manager, PersonModel _fp) { using (DefaultClient _client = new DefaultClient()) { _client.ClientCredential = CredentialCache.DefaultNetworkCredentials; _client.RefreshSchema(); List <RmResource> _res = _client.Enumerate("/Person[ObjectID='" + _fp.ObjectID + "']").ToList(); foreach (RmPerson _r in _res) { RmResourceChanges changes = new RmResourceChanges(_r); try { changes.BeginChanges(); if (string.IsNullOrWhiteSpace(manager)) { RmAttributeName _attr = new RmAttributeName("Manager"); _r.Attributes.Remove(_attr); } else { _r.Manager = new RmReference(manager); } _client.Put(changes); changes.AcceptChanges(); } catch { changes.DiscardChanges(); } } } }
public RmAttributeValue CreateRmAttributeValue(RmAttributeName attributeName) { if (IsMultiValued(attributeName)) { return(new RmAttributeValueMulti()); } else { return(new RmAttributeValueSingle()); } }
private DirectoryAccessChange BuildDirectoryAccessChange(RmAttributeName name, IComparable value) { DirectoryAccessChange retReqChange = new DirectoryAccessChange(); retReqChange.AttributeType = name.Name; XmlElement attributeValueElem = base.RmDoc.CreateElement(retReqChange.AttributeType, RmNamespace); attributeValueElem.InnerText = value.ToString(); retReqChange.AttributeValue.Values.Add(attributeValueElem); return(retReqChange); }
public bool IsReference(RmAttributeName attributeName) { RmAttributeInfo retValue = null; RmAttributeCache.TryGetValue(attributeName, out retValue); if (retValue == null) { return(false); } else { return(retValue.AttributeType == RmAttributeType.Reference); } }
public bool IsMultiValued(RmAttributeName attributeName) { RmAttributeInfo retValue = null; RmAttributeCache.TryGetValue(attributeName, out retValue); if (retValue == null) { return(false); } else { return(retValue.IsMultiValue); } }
/// <summary> /// GetAttributeType /// </summary> /// <param name="attributeName">Name of the attribute.</param> /// <returns>The FIM type of the attribute if found, null otherwise.</returns> /// <remarks>This method is only for testing, and should not be used /// directly by clients.</remarks> internal RmAttributeType?GetAttributeType(RmAttributeName attributeName) { RmAttributeInfo retValue = null; RmAttributeCache.TryGetValue(attributeName, out retValue); if (retValue == null) { return(null); } else { return(retValue.AttributeType); } }
DirectoryAccessChange BuildDirectoryAccessChange(RmAttributeName name, IComparable value) { if (value == null) { throw new ArgumentNullException("name", string.Format("Attribute '{0}' is null.", name)); } DirectoryAccessChange retReqChange = new DirectoryAccessChange(); retReqChange.AttributeType = name.Name; XmlElement attributeValueElem = base.RmDoc.CreateElement(retReqChange.AttributeType, RmNamespace); attributeValueElem.InnerText = value.ToString(); retReqChange.AttributeValue.Values.Add(attributeValueElem); return(retReqChange); }
/// <summary> /// Implement this method to ensure that custom attributes, i.e. /// attributes not defined in the default FIM schema, exist. /// </summary> protected void EnsureCustomAttributesExist(RmAttributeName attributeName, bool multiValued) { EnsureNotDisposed(); lock (attributes) { if (attributeName == null) { throw new ArgumentNullException("attributeName"); } if (attributes.ContainsKey(attributeName)) { return; } else { attributes.Add(attributeName, multiValued ? (RmAttributeValue) new RmAttributeValueMulti() : (RmAttributeValue) new RmAttributeValueSingle()); } } }
public bool IsRequired(String objectType, RmAttributeName attributeName) { Dictionary <RmAttributeName, RmAttributeInfo> attributeValue = null; RmObjectCache.TryGetValue(objectType, out attributeValue); if (attributeValue == null) { return(false); } else { RmAttributeInfo attributeInfo = null; attributeValue.TryGetValue(attributeName, out attributeInfo); if (attributeInfo == null) { return(false); } else { return(attributeInfo.IsRequired); } } }
private RmResource ConvertToResource(ExportObject exportObject) { var sourceObject = exportObject.ResourceManagementObject; var resource = new RmResource(); resource.ObjectType = sourceObject.ObjectType; foreach (var attribute in sourceObject.ResourceManagementAttributes) { var rmAttributeName = new RmAttributeName(attribute.AttributeName); var rmAttributeValue = attribute.IsMultiValue ? (RmAttributeValue) new RmAttributeValueMulti(attribute.Values) : (RmAttributeValue) new RmAttributeValueSingle(attribute.Value) ; if (rmAttributeValue.Value is string) { string s = (string)rmAttributeValue.Value; if (s.StartsWith("urn:uuid:")) { rmAttributeValue.Value = new RmReference(s); } } if (resource.ContainsKey(rmAttributeName)) { resource[rmAttributeName] = rmAttributeValue; } else { resource.Add(rmAttributeName, rmAttributeValue); } } return(resource); }
public RmGeneric CreateResource(ResponseGet getResponse, bool returnGenericType) { if (getResponse == null) { throw new ArgumentNullException("getResponse"); } lock (getResponse) { // look ahead for the type String objectType = null; foreach (PartialAttributeType partialAttribute in getResponse.PartialAttributes) { if (partialAttribute.Values.Count > 0) { String localName = partialAttribute.Values[0].LocalName; if (String.IsNullOrEmpty(localName)) { continue; } if (localName.Equals(ObjectType)) { objectType = partialAttribute.Values[0].InnerText; break; } } } if (objectType == null) { objectType = string.Empty; } RmGeneric rmResource = this.resourceTypeFactory.CreateResource(objectType, returnGenericType); // fill in the attribute values foreach (PartialAttributeType partialAttribute in getResponse.PartialAttributes) { RmAttributeName attributeName = null; RmAttributeValue newAttribute = null; if (partialAttribute.Values.Count > 0) { String localName = partialAttribute.Values[0].LocalName; if (String.IsNullOrEmpty(localName)) { continue; } else { attributeName = new RmAttributeName(localName); } } else { continue; } if (rmResource.TryGetValue(attributeName, out newAttribute) == false) { newAttribute = new RmAttributeValue(); rmResource.Add(new KeyValuePair <RmAttributeName, RmAttributeValue>(attributeName, newAttribute)); } // add values to the typed list foreach (XmlNode value in partialAttribute.Values) { IComparable newValue = this.ConstructAttributeValue(attributeName, value.InnerText); if (base.IsMultiValued(attributeName) == false) { newAttribute.Values.Clear(); } else { newAttribute.IsMultiValue = true; } if (attributeName.Name.Equals(ObjectType) || attributeName.Name.Equals(ObjectID)) { newAttribute.Values.Clear(); } newAttribute.IsReadOnly = value.IsReadOnly; newAttribute.IsRequired = base.IsRequired(objectType, attributeName); newAttribute.Values.Add(newValue); } } return(rmResource); } }
public RmFactory(XmlSchemaSet rmSchema) { if (rmSchema == null) { throw new ArgumentNullException("rmSchema"); } lock (rmSchema) { this.RmSchema = rmSchema; if (this.RmSchema.IsCompiled == false) { this.RmSchema.Compile(); } this.RmAttributeCache = new Dictionary <RmAttributeName, RmAttributeInfo>(); this.RmObjectCache = new Dictionary <string, Dictionary <RmAttributeName, RmAttributeInfo> >(); this.RmDoc = new XmlDocument(); this.RmNsManager = new XmlNamespaceManager(this.RmDoc.NameTable); this.RmNsManager.AddNamespace("rm", RmNamespace); foreach (XmlSchemaObject schemaObj in this.RmSchema.GlobalTypes.Values) { XmlSchemaComplexType schemaObjComplexType = schemaObj as XmlSchemaComplexType; if (schemaObjComplexType != null) { if (schemaObjComplexType.Name == null || schemaObjComplexType.Particle == null) { continue; } RmObjectCache[schemaObjComplexType.Name] = new Dictionary <RmAttributeName, RmAttributeInfo>(); XmlSchemaSequence schemaObjSequence = schemaObjComplexType.Particle as XmlSchemaSequence; if (schemaObjSequence != null) { foreach (XmlSchemaObject sequenceObj in schemaObjSequence.Items) { XmlSchemaElement sequenceElement = sequenceObj as XmlSchemaElement; if (sequenceElement != null) { RmAttributeInfo info = new RmAttributeInfo(); if (sequenceElement.MaxOccurs > Decimal.One) { info.IsMultiValue = true; } if (sequenceElement.MinOccurs > Decimal.Zero) { info.IsRequired = true; } String attributeTypeName = sequenceElement.ElementSchemaType.QualifiedName.Name.ToUpperInvariant(); if (attributeTypeName.Contains("COLLECTION")) { info.IsMultiValue = true; } if (attributeTypeName.Contains("REFERENCE")) { info.AttributeType = RmAttributeType.Reference; } else if (attributeTypeName.Contains("BOOLEAN")) { info.AttributeType = RmAttributeType.Boolean; } else if (attributeTypeName.Contains("INTEGER")) { info.AttributeType = RmAttributeType.Integer; } else if (attributeTypeName.Contains("DATETIME")) { info.AttributeType = RmAttributeType.DateTime; } else if (attributeTypeName.Contains("BINARY")) { info.AttributeType = RmAttributeType.Binary; } else { info.AttributeType = RmAttributeType.String; } RmAttributeName attributeName = new RmAttributeName(sequenceElement.Name); RmObjectCache[schemaObjComplexType.Name][attributeName] = info; RmAttributeCache[attributeName] = info; } } } } } } }
public static List <RmResource> GetResourceByQuery(ref SqlConnection conn, string requestorID, string xpath, string[] attributesToLoad, bool checkRights = true, int count = -1, int skip = 0) { XPathToSQLBuilder builder = new XPathToSQLBuilder(xpath, attributesToLoad, requestorID, conn); string sqlQuery = builder.BuildSQLQuery(checkRights, count, skip); var __resourceDict = new Dictionary <string, RmResource>(); using (SqlCommand cmd = new SqlCommand(sqlQuery, conn)) { using (SqlDataReader resourceReader = cmd.ExecuteReader()) { while (resourceReader.Read()) { var __dbRes = new DBResource() { ObjectID = resourceReader[0].ToString(), ObjectType = resourceReader[1].ToString(), AttributeName = resourceReader[2].ToString(), AttributeType = resourceReader[3].ToString(), AttributeValue = resourceReader[4] == null ? string.Empty : resourceReader[4].ToString(), DisplayName = resourceReader[5] == null ? string.Empty : resourceReader[5].ToString(), IsMultivalue = Convert.ToBoolean(resourceReader[6]) }; RmResource __resource = null; if (__resourceDict.ContainsKey(__dbRes.ObjectID)) { __resource = __resourceDict[__dbRes.ObjectID]; } else { __resource = new RmResource() { ObjectID = new RmReference(__dbRes.ObjectID), ObjectType = __dbRes.ObjectType, DisplayName = __dbRes.DisplayName }; __resourceDict.Add(__dbRes.ObjectID, __resource); } var __attName = new RmAttributeName(__dbRes.AttributeName); if (!__resource.ContainsKey(__attName)) { __resource.Attributes.Add(__attName, new RmAttributeValue() { Value = __dbRes.AttributeValue }); } else { if (!string.IsNullOrEmpty(__dbRes.AttributeValue)) { if (__resource[__attName].Values.Exists(v => v.ToString().Equals(__dbRes.AttributeValue, StringComparison.OrdinalIgnoreCase))) { __resource[__attName].Values.Add(__dbRes.AttributeValue); } } } } } } return(__resourceDict.Values.ToList()); }
public bool modifyResourceAttribute(RmResource resource, RmAttributeName attName, RmAttributeValue attValue, bool overwriteMultivalue = false, bool allowOverwrite = true, bool allowNull = true) { bool modified = false; if (resource == null || attName == null || string.IsNullOrEmpty(attName.Name)) { return(modified); } if (attValue == null || attValue.Value == null) { if (allowOverwrite && allowNull) { if (resource.ContainsKey(attName)) { resource.Remove(attName); modified = true; } } } else { if (resource.ContainsKey(attName)) { if (attValue.IsMultiValue) { if (resource[attName].Values == null || resource[attName].Values.Count == 0) { resource[attName] = attValue; modified = true; } else { if (overwriteMultivalue) { modified = true; resource[attName].Value = attValue.Value; resource[attName].Values = attValue.Values; } else if (allowOverwrite) { // Compare the multivalues if (resource[attName].Values.Where(v1 => !attValue.Values.Exists(v2 => v2.ToString().Equals(v1.ToString()))).Union( attValue.Values.Where(v1 => !resource[attName].Values.Exists(v2 => v2.ToString().Equals(v1.ToString())))).Count() != 0) { modified = true; } resource[attName].Values.RemoveAll(v1 => !attValue.Values.Exists(v2 => v2.ToString().Equals(v1.ToString()))); resource[attName].Values.AddRange( attValue.Values.Where(v1 => !resource[attName].Values.Exists(v2 => v2.ToString().Equals(v1.ToString())))); } } } else { if (resource[attName].Value == null || string.IsNullOrEmpty(resource[attName].Value.ToString())) { resource[attName] = attValue; modified = true; } else { if (allowOverwrite) { if (!resource[attName].Value.Equals(attValue.Value)) { resource[attName].Value = attValue.Value; modified = true; } } } } } else { resource.Add(attName, attValue); modified = true; } } return(modified); }
protected IComparable ConstructAttributeValue(RmAttributeName attributeName, String innerText) { if (innerText == null) { return(null); } RmAttributeInfo info = null; if (base.RmAttributeCache.TryGetValue(attributeName, out info) == false) { if (attributeName.Name.Equals(ObjectID)) { return(new RmReference(innerText)); } else { return(innerText); } } try { switch (info.AttributeType) { case RmAttributeType.String: return(innerText); case RmAttributeType.DateTime: return(DateTime.Parse(innerText)); case RmAttributeType.Integer: return(Int32.Parse(innerText)); case RmAttributeType.Reference: return(new RmReference(innerText)); case RmAttributeType.Binary: return(new RmBinary(innerText)); case RmAttributeType.Boolean: return(Boolean.Parse(innerText)); default: return(innerText); } } catch (FormatException ex) { throw new ArgumentException( String.Format( "Failed to parse attribute {0} with value {1} into type {2}. Please ensure the resource management schema is up to date.", attributeName, innerText, info.AttributeType.ToString()), ex); } catch (System.Text.EncoderFallbackException ex) { throw new ArgumentException( String.Format( "Failed to convert the string on binary attribute {0} into byte array.", attributeName), ex); } }
public List <RmGeneric> CreateResource(ResponsePull pullOrEnumerateResponse, bool returnGenericType) { if (pullOrEnumerateResponse == null) { throw new ArgumentNullException("pullOrEnumerateResponse"); } if (pullOrEnumerateResponse.Items == null || pullOrEnumerateResponse.Items.Values == null) { return(new List <RmGeneric>()); } lock (pullOrEnumerateResponse) { List <RmGeneric> retList = new List <RmGeneric>(); foreach (XmlNode obj in pullOrEnumerateResponse.Items.Values) { // look ahead for the type info; string objectType = null; foreach (XmlNode child in obj.ChildNodes) { if (child.NodeType == XmlNodeType.Element) { if (child.LocalName.Equals(@"ObjectType")) { objectType = child.InnerText; break; } } } if (objectType == null) { objectType = String.Empty; } RmGeneric rmResource = this.resourceTypeFactory.CreateResource(objectType, returnGenericType); // now add the attributes to the resource object foreach (XmlNode child in obj.ChildNodes) { if (child.NodeType == XmlNodeType.Element) { RmAttributeName attributeName = new RmAttributeName(child.LocalName); IComparable attributeValue = this.ConstructAttributeValue(attributeName, child.InnerText); if (attributeValue == null) { continue; } RmAttributeValue newAttribute = null; if (rmResource.TryGetValue(attributeName, out newAttribute) == false) { newAttribute = new RmAttributeValue(); rmResource[attributeName] = newAttribute; } if (base.IsMultiValued(attributeName) == false) { newAttribute.Values.Clear(); } else { newAttribute.IsMultiValue = true; } if (attributeName.Name.Equals(ObjectType) || attributeName.Name.Equals(ObjectID)) { newAttribute.Values.Clear(); } newAttribute.IsReadOnly = child.IsReadOnly; newAttribute.IsRequired = base.IsRequired(objectType, attributeName); newAttribute.Values.Add(attributeValue); } } retList.Add(rmResource); } return(retList); } }
/// <summary> /// Returns a generic RmResource object from the getResponse object. /// </summary> /// <param name="getResponse">The get response from the server.</param> /// <returns>The RmResource object with the attributes returned in getResponse.</returns> public RmResource CreateResource(GetResponse getResponse) { if (getResponse == null) { throw new ArgumentNullException("getResponse"); } if (getResponse.BaseObjectSearchResponse == null) { throw new ArgumentNullException("getResponse.BaseObjectSearchResponse"); } lock (getResponse) { // look ahead for the type String objectType = null; foreach (PartialAttributeType partialAttribute in getResponse.BaseObjectSearchResponse.PartialAttributes) { if (partialAttribute.Values.Count > 0) { String localName = partialAttribute.Values[0].LocalName; if (String.IsNullOrEmpty(localName)) { continue; } if (localName.Equals(ObjectType)) { objectType = partialAttribute.Values[0].InnerText; break; } } } if (objectType == null) { objectType = string.Empty; } RmResource rmResource = this.resourceTypeFactory.CreateResource(objectType); // fill in the attribute values foreach (PartialAttributeType partialAttribute in getResponse.BaseObjectSearchResponse.PartialAttributes) { RmAttributeName attributeName = null; RmAttributeValue newAttribute = null; if (partialAttribute.Values.Count > 0) { String localName = partialAttribute.Values[0].LocalName; if (String.IsNullOrEmpty(localName)) { continue; } else { attributeName = new RmAttributeName(localName); } } else { continue; } if (rmResource.TryGetValue(attributeName, out newAttribute) == false) { newAttribute = CreateRmAttributeValue(attributeName); // PATCHED: the following line was missing rmResource[attributeName] = newAttribute; } // add values to the typed list foreach (XmlNode value in partialAttribute.Values) { IComparable newValue = this.ConstructAttributeValue(attributeName, value.InnerText); if (base.IsMultiValued(attributeName) == false) { newAttribute.Values.Clear(); } if (attributeName.Name.Equals(ObjectType) || attributeName.Name.Equals(ObjectID)) { newAttribute.Values.Clear(); } newAttribute.Values.Add(newValue); } } return(rmResource); } }