Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] Group userInputGroup)
        {
            // requestor is a group administration and can create groups
            if (!(await _securityHelper.IsGroupAdmin()))
            {
                return(new HttpStatusCodeResult(403)); // Forbidden;
            }
            //TODO: group parameter validation

            // get current user
            var currentUser = await _securityHelper.GetOrCreateCurrentUser();

            // build the resource group object
            var group = new ScampResourceGroup()
            {
                Id          = Guid.NewGuid().ToString(),
                Name        = userInputGroup.Name,
                Description = userInputGroup.Description,
                Budget      = new ScampGroupBudget()
                {
                    OwnerId               = currentUser.Id,
                    OwnerName             = currentUser.Name,
                    unitsBudgeted         = userInputGroup.Budget.unitsBudgeted,
                    DefaultUserAllocation = userInputGroup.Budget.defaultUserBudget,
                    EndDate               = userInputGroup.Budget.expiryDate
                }
            };

            await _groupRepository.CreateGroup(group);

            // after we know the user docs have completed successfully, add the volatile storage records
            Task[] tasks = new Task[2]; // collection to hold the parallel tasks

            // create group volatile storage entries
            var newGrpBudget = new GroupBudgetState(group.Id)
            {
                UnitsBudgetted = userInputGroup.Budget.unitsBudgeted,
                UnitsAllocated = userInputGroup.Budget.defaultUserBudget,
                UnitsUsed      = 0
            };

            tasks[0] = _volatileStorageController.AddGroupBudgetState(newGrpBudget);

            // create volatile storage budget entry for user/group
            var newUsrBudget = new UserBudgetState(currentUser.Id, group.Id)
            {
                UnitsBudgetted = group.Budget.DefaultUserAllocation,
                UnitsUsed      = 0
            };

            tasks[1] = _volatileStorageController.AddUserBudgetState(newUsrBudget);

            // wait for both operations to complete
            Task.WaitAll(tasks);

            return(new ObjectResult(Map(group))
            {
                StatusCode = 200
            });
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddUserToGroup(string groupId, [FromBody] UserSummary newUser)
        {
            if (!await _securityHelper.CurrentUserCanManageGroup(groupId))
            {
                return(new HttpStatusCodeResult(403)); // Forbidden
            }

            string userId = newUser.Id;

            // get group details
            var rscGroup = await _groupRepository.GetGroup(groupId);

            if (rscGroup == null)
            {
                return(new ObjectResult("designated group does not exist")
                {
                    StatusCode = 400
                });
            }

            // make sure user isn't already in group
            IEnumerable <ScampUserGroupMbrship> userList = from ur in rscGroup.Members
                                                           where ur.Id == userId
                                                           select ur;

            if (userList.Count() > 0) // user is already in the list
            {
                return new ObjectResult("designated user is already a member of specified group")
                       {
                           StatusCode = 400
                       }
            }
            ;

            // create the user if they don't exist
            //TODO: https://github.com/SimpleCloudManagerProject/SCAMP/issues/247
            if (!(await _userRepository.UserExists(userId)))
            {
                // build user object
                var tmpUser = new ScampUser(newUser);

                // insert into database
                await _userRepository.CreateUser(tmpUser);
            }

            //TODO: Issue #152
            // check to make sure enough remains in the group allocation to allow add of user

            // create volatile storage budget entry for user
            var newBudget = new UserBudgetState(userId, groupId)
            {
                //TODO: Take into account the budget potentially sent in POST body
                UnitsBudgetted = rscGroup.Budget.DefaultUserAllocation,
                UnitsUsed      = 0
            };
            await _volatileStorageController.AddUserBudgetState(newBudget);

            newUser.unitsBudgeted = newBudget.UnitsBudgetted;

            // create document updates
            await _groupRepository.AddUserToGroup(groupId, userId, false);

            //TODO: Issue #152
            // update group budget allocation to reflect addition of new user


            // return list
            return(new ObjectResult(newUser)
            {
                StatusCode = 200
            });
        }