Beispiel #1
0
        /// <summary>
        /// Creates the new community in Layerscape with the given details passed in CommunitiesView instance.
        /// </summary>
        /// <param name="communityDetail">Details of the community</param>
        /// <param name="userId">User Identity</param>
        /// <returns>Id of the community created. Returns -1 is creation is failed.</returns>
        public async void UpdateCommunity(CommunityDetails communityDetail, long userId)
        {
            // Make sure communityDetails is not null
            this.CheckNotNull(() => new { communityDetails = communityDetail });

            var community =  _communityRepository.GetItem((Community c) => c.CommunityID == communityDetail.ID && c.IsDeleted == false);

            // Make sure community is not null
            this.CheckNotNull(() => new { community });

            var userRole = GetCommunityUserRole(community.CommunityID, userId);
            if (!CanEditDeleteCommunity(community, userId, userRole))
            {
                throw new HttpException(401, Resources.NoPermissionUpdateCommunityMessage);
            }

            // For deleted communities, do not updates the changes.
            if (community.IsDeleted.HasValue && !community.IsDeleted.Value)
            {
                // Do not let the user to change the Community as public in case if it is offensive.
                // This scenario might happen when the edit page is left open or cached and meantime Community is marked as offensive 
                // by the Site Admin.
                if (community.IsOffensive.HasValue && (bool)community.IsOffensive && communityDetail.AccessTypeID == (int)AccessType.Public)
                {
                    communityDetail.AccessTypeID = (int)AccessType.Private;
                    communityDetail.IsOffensive = true;
                }

                Mapper.Map(communityDetail, community);

                community.ModifiedByID = userId;
                community.ModifiedDatetime = DateTime.UtcNow;

                // 1. Add Thumbnail to blob
                if (communityDetail.Thumbnail != null)
                {
                    if (communityDetail.Thumbnail.AzureID != Guid.Empty && community.ThumbnailID != communityDetail.Thumbnail.AzureID)
                    {
                        if (MoveThumbnail(communityDetail.Thumbnail))
                        {
                            community.ThumbnailID = communityDetail.Thumbnail.AzureID;
                        }
                        else
                        {
                            community.ThumbnailID = Guid.Empty;
                        }
                    }
                    else if (communityDetail.Thumbnail.AzureID == Guid.Empty)
                    {
                        community.ThumbnailID = Guid.Empty;
                    }
                }

                // 2. Update Tag details. This will also take care of creating tags if they are not there in the Layerscape database.
                SetCommunityTags(communityDetail.Tags, community);

                // 3. Update User role details. Any change in parent, roles need to be updated.
                //      Even if there was no parent before, need to check if any parent is mentioned now.
                // Get the previous parent if any. Note that there will be only one parent.
                long previousParent = 0;
                if (community.CommunityRelation1.Count == 1)
                {
                    previousParent = community.CommunityRelation1.ElementAt(0).ParentCommunityID;
                }

                if (communityDetail.ParentID != previousParent)
                {
                    _userCommunitiesRepository.InheritParentRoles(community, communityDetail.ParentID);
                }

                // 4. Update Parent Community details in case if Parent is specified

                // TODO: Need to check if we can move the community.

                // Few things to be noted:
                // a) Obviously the count to be 0 or 1 always.
                // b) A community can be child of only once parent community or folder and hence only one CommunityRelation1
                if (community.CommunityRelation1.Count > 0 && community.CommunityRelation1.ElementAt(0).ParentCommunityID != communityDetail.ParentID)
                {
                    community.CommunityRelation1.Clear();
                }

                if (communityDetail.ParentID > 0 && community.CommunityRelation1.Count == 0)
                {
                    // Add Parent Community details again
                    var communityRelation = new CommunityRelation();
                    communityRelation.ParentCommunityID = communityDetail.ParentID;
                    communityRelation.ChildCommunityID = communityDetail.ID;

                    // TODO: Need to rename the Data Model property CommunityRelation1 with a more meaningful name.
                    // Note that the relation to be added is to CommunityRelation1 since the current Community is Child.
                    // When the current community is parent, it's relation to be added in CommunityRelation.
                    community.CommunityRelation1.Add(communityRelation);
                }

                // Mark the Community as updated
                _communityRepository.Update(community);

                // TODO: Need to check the concurrency scenarios.
                // Save all the changes made.
                _communityRepository.SaveChanges();
            }
            else
            {
                // TODO: Need to throw exception informing user that the community is deleted.
            }
        }
Beispiel #2
0
        public long CreateCommunity(CommunityDetails communityDetail)
        {
            // Make sure communityDetails is not null
            this.CheckNotNull(() => new { communityDetails = communityDetail });

            var userRole = GetCommunityUserRole(communityDetail.ParentID, communityDetail.CreatedByID);
            if (!CanCreateCommunity(communityDetail.ParentID, userRole))
            {
                throw new HttpException(401, Resources.NoPermissionCreateCommunityMessage);
            }

            // In case if the community getting created is "User" type, check that the same user already has a "User" community associated with him.
            // There should be only one "User" community to be created per user.
            if (communityDetail.CommunityType == CommunityTypes.User)
            {
                var existingUserCommunity = _communityRepository.GetItem(
                    c => c.CommunityTypeID == (int) CommunityTypes.User && c.CreatedByID == communityDetail.CreatedByID);

                if (existingUserCommunity != null)
                {
                    return existingUserCommunity.CommunityID;
                }
            }

            // 1. Add Community details to the community object.
            var community = new Community();
            Mapper.Map(communityDetail, community);

            // While creating the community, IsDeleted to be false always.
            community.IsDeleted = false;

            community.CreatedDatetime = community.ModifiedDatetime = DateTime.UtcNow;

            // 2. Add Thumbnail to blob
            if (communityDetail.Thumbnail != null && communityDetail.Thumbnail.AzureID != Guid.Empty)
            {
                if (MoveThumbnail(communityDetail.Thumbnail))
                {
                    community.ThumbnailID = communityDetail.Thumbnail.AzureID;
                }
            }

            // 3. Add Tag details. This will also take care of creating tags if they are not there in the Layerscape database.
            SetCommunityTags(communityDetail.Tags, community);

            var parentAddedToUserRole = false;
            if (communityDetail.ParentID > 0)
            {
                // 4. Add Parent Community details
                var communityRelation = new CommunityRelation
                {
                    ParentCommunityID = communityDetail.ParentID,
                    ChildCommunityID = communityDetail.ID
                };

                // TODO: Need to rename the Data Model property CommunityRelation1 with a more meaningful name.
                // Note that the relation to be added is to CommunityRelation1 since the current Community is Child.
                // When the current community is parent, it's relation to be added in CommunityRelation.
                community.CommunityRelation1.Add(communityRelation);

                var parentCommunity = _communityRepository.GetItem(c => c.CommunityID == communityDetail.ParentID);
                if (parentCommunity != null && parentCommunity.UserCommunities.Count > 0)
                {
                    parentCommunity.ModifiedByID = communityDetail.CreatedByID;
                    parentCommunity.ModifiedDatetime = DateTime.UtcNow;

                    // 5. Inherit Parent Permission Details
                    foreach (var communityUserRole in parentCommunity.UserCommunities)
                    {
                        var userCommunityRole = new UserCommunities
                        {
                            CommunityId = communityDetail.ID,
                            UserID = communityUserRole.UserID,
                            RoleID = communityUserRole.RoleID,
                            IsInherited = true,
                            CreatedDatetime = DateTime.UtcNow
                        };

                        // Add the current community to the use role

                        // Add the existing users along with their roles

                        community.UserCommunities.Add(userCommunityRole);

                        if (communityUserRole.UserID == communityDetail.CreatedByID)
                        {
                            parentAddedToUserRole = true;
                        }
                    }
                }
            }

            if (!parentAddedToUserRole)
            {
                // 6. Add Owner Permission Details only if its not already inherited.
                var userCommunity = new UserCommunities
                {
                    CommunityId = communityDetail.ID,
                    UserID = communityDetail.CreatedByID,
                    CreatedDatetime = DateTime.UtcNow,
                    RoleID = (int) UserRole.Owner,
                    IsInherited = false
                };

                // User who is creating the community is the owner.
                community.UserCommunities.Add(userCommunity);
            }

            // Add the community to the repository
            _communityRepository.Add(community);

            // Save all the changes made.
            _communityRepository.SaveChanges();

            return community.CommunityID;
        }