Example #1
0
        internal static void SetOwner(XrmDb db, Security dataMethods, MetadataSkeleton metadata, Entity entity, EntityReference owner)
        {
            var ownershipType = metadata.EntityMetadata.GetMetadata(entity.LogicalName).OwnershipType;

            if (!ownershipType.HasValue)
            {
                throw new MockupException($"No ownership type set for '{entity.LogicalName}'");
            }

            if (ownershipType.Value.HasFlag(OwnershipTypes.UserOwned) || ownershipType.Value.HasFlag(OwnershipTypes.TeamOwned))
            {
                if (db.GetDbRowOrNull(owner) == null)
                {
                    throw new FaultException($"Owner referenced with id '{owner.Id}' does not exist");
                }

                var prevOwner = entity.Attributes.ContainsKey("ownerid") ? entity["ownerid"] : null;
                entity["ownerid"] = owner;

                if (!dataMethods.HasPermission(entity, AccessRights.ReadAccess, owner))
                {
                    entity["ownerid"] = prevOwner;
                    throw new FaultException($"Trying to assign '{entity.LogicalName}' with id '{entity.Id}'" +
                                             $" to '{owner.LogicalName}' with id '{owner.Id}', but owner does not have read access for that entity");
                }

                entity["owningbusinessunit"] = null;
                entity["owninguser"]         = null;
                entity["owningteam"]         = null;


                if (entity.LogicalName != LogicalNames.SystemUser && entity.LogicalName != LogicalNames.Team)
                {
                    if (owner.LogicalName == LogicalNames.SystemUser && ownershipType.Value.HasFlag(OwnershipTypes.UserOwned))
                    {
                        entity["owninguser"] = owner;
                    }
                    else if (owner.LogicalName == "team")
                    {
                        entity["owningteam"] = owner;
                    }
                    else
                    {
                        throw new MockupException($"Trying to give owner to {owner.LogicalName} but ownershiptype is {ownershipType.ToString()}");
                    }
                    entity["owningbusinessunit"] = GetBusinessUnit(db, owner);
                }
            }
        }
Example #2
0
 internal bool HasPermission(EntityReference entityRef, AccessRights access, EntityReference principleRef)
 {
     return(security.HasPermission(entityRef, access, principleRef));
 }