Beispiel #1
0
        private void InitializeDB()
        {
            this.OrganizationId   = Guid.NewGuid();
            this.OrganizationName = "MockupOrganization";

            // Setup currencies
            var currencies = new List <Entity>();

            foreach (var entity in metadata.Currencies)
            {
                Utility.RemoveAttribute(entity, "createdby", "modifiedby", "organizationid", "modifiedonbehalfby", "createdonbehalfby");
                if (db.GetEntityOrNull(entity.ToEntityReference()) == null)
                {
                    db.Add(entity);
                }
            }

            // Setup root business unit
            var rootBu = metadata.RootBusinessUnit;

            if (db.GetEntityOrNull(metadata.RootBusinessUnit.ToEntityReference()) == null)
            {
                rootBu["name"] = "RootBusinessUnit";
                rootBu.Attributes.Remove("organizationid");
                this.db.Add(rootBu, false);
            }
            this.RootBusinessUnitRef = rootBu.ToEntityReference();

            // Setup admin user
            var admin = new Entity(LogicalNames.SystemUser)
            {
                Id = Guid.NewGuid()
            };

            this.AdminUserRef = admin.ToEntityReference();

            admin["firstname"]      = "";
            admin["lastname"]       = "SYSTEM";
            admin["businessunitid"] = RootBusinessUnitRef;
            this.db.Add(admin);

            // Setup default team for root business unit
            var allTeams = db.GetEntities("team");

            if (!allTeams.Any(x => (!x.Contains("teamtype") || x.GetAttributeValue <OptionSetValue>("teamtype").Value == 0) &&
                              x.GetAttributeValue <EntityReference>("businessunitid").Id == RootBusinessUnitRef.Id &&
                              x.GetAttributeValue <bool>("isdefault")))
            {
                var defaultTeam = Utility.CreateDefaultTeam(rootBu, AdminUserRef);
                this.db.Add(defaultTeam);

                // Adding admin user to root business unit default team
                var teamMembership = new Entity(LogicalNames.TeamMembership);
                teamMembership["teamid"]       = defaultTeam.Id;
                teamMembership["systemuserid"] = admin.Id;
                teamMembership.Id = Guid.NewGuid();
                this.db.Add(teamMembership);
            }
        }
Beispiel #2
0
        internal static EntityReference GetBusinessUnit(IXrmDb db, EntityReference owner)
        {
            var user = db.GetEntityOrNull(owner);

            if (user == null)
            {
                return(null);
            }
            var buRef = user.GetAttributeValue <EntityReference>("businessunitid");
            var bu    = db.GetEntityOrNull(buRef);

            if (bu == null)
            {
                return(null);
            }
            buRef.Name = bu.GetAttributeValue <string>("name");
            return(buRef);
        }
Beispiel #3
0
        internal static void SetOwner(IXrmDb 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.GetEntityOrNull(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);
                }
            }
        }
Beispiel #4
0
        internal bool HasPermission(Entity entity, AccessRights access, EntityReference caller)
        {
            // check if system has no security roles for this entity: it is a case with linkentities which have no security roles
            if (!SecurityRoles.Any(s => s.Value.Privileges.Any(p => p.Key == entity.LogicalName)))
            {
                return(true);
            }

            if (caller.Id == Core.AdminUserRef.Id)
            {
                return(true);
            }

            // check if the caller has the permissions needed for access
            if (HasCallerPermission(entity, access, caller))
            {
                return(true);
            }

            // check if any of the Teams that the caller is a member of has access
            if (HasTeamMemberPermission(entity, access, caller))
            {
                return(true);
            }

#if !(XRM_MOCKUP_2011)
            // check if any of the Teams that the caller is a member of has access
            if (HasAccessTeamMemberPermission(entity, access, caller))
            {
                return(true);
            }
#endif

            // check if there are any shares of the entity with the caller
            if (HasSharePermission(entity, access, caller))
            {
                return(true);
            }

            // not part of HasPermissions check - TODO: move to Utility: SetOwner
            var parentChangeRelationships = Metadata.EntityMetadata.GetMetadata(entity.LogicalName).ManyToOneRelationships
                                            .Where(r => r.CascadeConfiguration.Reparent == CascadeType.Cascade || r.CascadeConfiguration.Reparent == CascadeType.Active)
                                            .Where(r => entity.Attributes.ContainsKey(r.ReferencingAttribute));

            //cope with entity[pcr.ReferencingAttribute] being null
            foreach (var pcr in parentChangeRelationships)
            {
                if (entity.Contains(pcr.ReferencingAttribute) && entity[pcr.ReferencingAttribute] != null)
                {
                    var refEntity = db.GetEntityOrNull(new EntityReference(pcr.ReferencedEntity, Utility.GetGuidFromReference(entity[pcr.ReferencingAttribute])));
                    if (refEntity != null)
                    {
                        if (!Core.GetEntityMetadata(refEntity.LogicalName).Attributes.Any(x => x.LogicalName == "ownerid"))
                        {
                            return(false);
                        }
                        else if (refEntity.GetAttributeValue <EntityReference>("ownerid").Id == caller.Id)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }