Esempio n. 1
0
        public void RemoveAssetFromLightbox(int lightboxId, int assetId, string additionalNotes)
        {
            Lightbox lb = GetLightboxById(lightboxId);

            if (EntitySecurityManager.CanManageLightbox(User, lb))
            {
                if (LightboxContainsAsset(lb, assetId))
                {
                    foreach (LightboxAsset lba in lb.GetLightboxAssetList())
                    {
                        if (lba.AssetId == assetId)
                        {
                            LightboxAsset.Delete(lba.LightboxAssetId);

                            AuditLogManager.LogAssetAction(assetId, User, AuditAssetAction.RemovedFromLightbox);

                            string notes = string.Format("Removed AssetId: {0} from LightboxId: {1}", assetId, lightboxId);

                            if (!StringUtils.IsBlank(additionalNotes))
                            {
                                notes += string.Format(". {0}", additionalNotes);
                            }

                            AuditLogManager.LogUserAction(User, AuditUserAction.RemoveFromLightbox, notes);
                        }
                    }
                }
            }
            else
            {
                m_Logger.DebugFormat("User: {0} (UserId: {1}) tried to remove AssetId: {2} from LightboxId: {3} but couldn't due to insufficient permissions to manage ths lightbox", User.FullName, User.UserId, assetId, lightboxId);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a copy of the lightbox asset to the lightbox, if it does not contain the asset
        /// </summary>
        public void AddAssetToLightbox(Lightbox lightbox, LightboxAsset lightboxAsset)
        {
            if (!lightbox.IsNull && !lightboxAsset.IsNull)
            {
                if (EntitySecurityManager.CanManageLightbox(User, lightbox))
                {
                    if (!LightboxContainsAsset(lightbox, lightboxAsset.AssetId))
                    {
                        LightboxAsset lba = LightboxAsset.New();
                        lba.LightboxId = lightbox.LightboxId.GetValueOrDefault();
                        lba.AssetId    = lightboxAsset.AssetId;
                        lba.Notes      = lightboxAsset.Notes;
                        lba.CreateDate = DateTime.Now;
                        LightboxAsset.Update(lba);

                        AuditLogManager.LogAssetAction(lightboxAsset.AssetId, User, AuditAssetAction.AddedToLightbox);
                        AuditLogManager.LogUserAction(User, AuditUserAction.AddToLightbox, string.Format("Added AssetId: {0} to LightboxId: {1}", lightboxAsset.AssetId, lightbox.LightboxId.GetValueOrDefault()));
                    }
                }
                else
                {
                    m_Logger.DebugFormat("User: {0} (UserId: {1}) tried to add AssetId: {2} to LightboxId: {3} but couldn't due to insufficient permissions to manage ths lightbox", User.FullName, User.UserId, lightboxAsset.AssetId, lightbox.LightboxId.GetValueOrDefault());
                }
            }
        }
Esempio n. 3
0
        public void MergeLightbox(int sourceLightboxId, int targetLightboxId, bool removeSource)
        {
            if (sourceLightboxId == targetLightboxId)
            {
                throw new InvalidLightboxException("source and target lightbox cannot be the same");
            }

            Lightbox source = GetLightboxById(sourceLightboxId);
            Lightbox target = GetLightboxById(targetLightboxId);

            if (source.IsLinked)
            {
                throw new InvalidLightboxException("cannot use linked lightboxes as the source");
            }

            if (target.IsLinked)
            {
                throw new InvalidLightboxException("cannot use linked lightboxes as the target");
            }

            if (removeSource)
            {
                if (source.IsDefault)
                {
                    throw new InvalidLightboxException("source lightbox cannot removed as it is the default lightbox");
                }

                if (!EntitySecurityManager.CanManageLightbox(User, source))
                {
                    throw new InvalidLightboxException("source lightbox cannot removed as you do not have permission");
                }
            }

            foreach (LightboxAsset lba in source.GetLightboxAssetList())
            {
                AddAssetToLightbox(targetLightboxId, lba);
            }

            if (removeSource)
            {
                RemoveLightbox(sourceLightboxId);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Validates the cart to ensure it is ready for ordering
        /// </summary>
        private static void ValidateCart(IEnumerable <Cart> cartItems, User user)
        {
            int availableCount = cartItems.Count(cartItem => AssetManager.GetAssetStatusForUser(cartItem.Asset, user) == AssetStatus.Available);

            if (availableCount == 0)
            {
                throw new InvalidOrderException("Cart does not contain any available assets");
            }

            if (cartItems.Any(cartItem => cartItem.RequiredByDate.HasValue && cartItem.RequiredByDate.Value < DateTime.Now.Date))
            {
                throw new InvalidOrderException("Required by date cannot be before today.  Please leave blank or choose new date.");
            }

            if ((from cartItem in cartItems
                 let asset = cartItem.Asset
                             let isAvailable = (AssetManager.GetAssetStatusForUser(asset, user) == AssetStatus.Available)
                                               let isRestricted = EntitySecurityManager.IsAssetRestricted(user, asset)
                                                                  where isAvailable && isRestricted && StringUtils.IsBlank(cartItem.Notes)
                                                                  select cartItem).Any())
            {
                throw new InvalidOrderException("Assets requiring approval must have usage notes specified.");
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Creates an order from the user's cart.
        /// </summary>
        public static Order CreateOrderFromCart(User user)
        {
            // Create new cart manager for easy access to cart
            CartManager cm = new CartManager(user);

            // Ensure that the cart contains orderable items
            ValidateCart(cm.CartList, user);

            // Create the order
            Order order = Order.New();

            order.UserId    = user.UserId.GetValueOrDefault();
            order.OrderDate = DateTime.Now;

            // Ensure there's no order items
            Debug.Assert(order.OrderItemList.Count == 0);

            // Now add the cart items to it
            foreach (Cart cartItem in cm.CartList)
            {
                Asset asset = cartItem.Asset;

                // Get the asset status for the cart item
                AssetStatus assetStatus = AssetManager.GetAssetStatusForUser(asset, user);

                // Only add assets that are actually available
                // (Ignore withdrawn and expired assets)
                if (assetStatus == AssetStatus.Available)
                {
                    // Set the order item status based on whether the item is restricted or not
                    bool isRestricted = EntitySecurityManager.IsAssetRestricted(user, asset);

                    // Create new order item
                    OrderItem orderItem = OrderItem.New();

                    // Order items that are for restricted assets are set to awaiting approval
                    // and need to be approved by the user who uploaded them by default
                    // (though this can change, and be reassigned to the BU admin or superadmin,
                    // as per the workflow and scheduled scripts)
                    if (isRestricted)
                    {
                        orderItem.OrderItemStatus  = OrderItemStatus.AwaitingApproval;
                        orderItem.AssignedToUserId = asset.UploadedByUser.GetOrderItemApproverUserId();
                    }
                    else
                    {
                        orderItem.OrderItemStatus  = OrderItemStatus.Preapproved;
                        orderItem.AssignedToUserId = null;
                    }

                    orderItem.AssetId        = cartItem.AssetId;
                    orderItem.Notes          = cartItem.Notes;
                    orderItem.RequiredByDate = cartItem.RequiredByDate;
                    orderItem.CreateDate     = DateTime.Now;
                    order.OrderItemList.Add(orderItem);
                }
            }

            // Save the order
            SaveOrder(order);

            // Log which assets were ordered
            foreach (OrderItem orderItem in order.OrderItemList)
            {
                AuditLogManager.LogAssetAction(orderItem.AssetId, user, AuditAssetAction.Ordered, string.Format("OrderId: {0}", orderItem.OrderId));
            }

            // Log order against user
            AuditLogManager.LogUserAction(user, AuditUserAction.OrderAssets, string.Format("Placed order, with OrderId: {0} containing {1} assets", order.OrderId, order.OrderItemList.Count));

            // Remove the items from the cart
            cm.EmptyCart(false);

            // Complete the order if all items have been processed
            if (AllOrderItemsProcessed(order))
            {
                // Nothing in the order required approval, so automatically complete it
                CompleteOrder(order);
            }
            else
            {
                // Fire event
                if (OrderCreated != null)
                {
                    OrderCreated(null, new OrderEventArgs(order));
                }
            }

            return(order);
        }