Esempio n. 1
0
        /// <summary>
        /// Gets the Member object that represents the currently executing security context.
        /// </summary>
        /// <param name="store">Store object.</param>
        /// <param name="domainID">The domain used to map the current user to.</param>
        /// <param name="createMember">If true, creates Member object if it does not exist.</param>
        /// <returns>A Member object that represents the currently executing security context.</returns>
        public Member GetCurrentMember(Store store, string domainID, bool createMember)
        {
            log.Debug("GetCurrentMember domain id is : {0}", domainID);
            // See if there is a currently impersonating user.
            Member member = ImpersonationMember;

            if (member == null)
            {
                // This collection is not currently being impersonated, go look up the Member object of
                // the current identity in the store.
                Identity identity = store.CurrentUser;
                string   userID   = identity.GetUserIDFromDomain(domainID);
                if (userID == null)
                {
                    // The domain mapping has to exist or it means that we never were invited to this domain.
                    throw new DoesNotExistException(String.Format("There is no identity mapping for identity {0} to domain {1}.", identity.ID, domainID));
                }

                // Check in the local store to see if there is an existing member.
                member = GetMember(userID);
                if (member == null)
                {
                    // The Member object does not exist, we were specified to create it with full rights.
                    // If this is a collection in a proxy state, create a special member for it with read
                    // only rights. This member will not be committable and should be treated as a read-only
                    // object.
                    string userName = identity.Name;

                    // Get the member that is represented in the domain.
                    Domain domain = store.GetDomain(collection.Domain);
                    if (domain != null)
                    {
                        // Find the member in the domain.
                        Member domainMember = domain.GetMemberByID(userID);
                        if (domainMember != null)
                        {
                            userName = domainMember.Name;
                        }
                    }

                    if (createMember)
                    {
                        member         = new Member(userName, userID, Access.Rights.Admin);
                        member.IsOwner = true;
                    }
                    else
                    {
                        // Member is not part of the cllection. Check whether it is
                        // shared with the Members group.
                        string[] IDs = domain.GetMemberFamilyList(userID);
                        foreach (string id in IDs)
                        {
                            member = collection.GetMemberByID(id);
                            if (member != null)
                            {
                                break;
                            }
                        }
                        if (member == null)
                        {
                            throw new DoesNotExistException(String.Format("The identity {0} - ID: {1} is not a member of collection {2} - ID: {3}.", userName, userID, collection.Name, collection.ID));
                        }
                    }
                }
            }

            return(member);
        }