Ejemplo n.º 1
0
        public async Task <int> NewAsync(string name, string description)
        {
            var userId = _ephItUser.Register().UserId;

            // This will return the new scriptId and then the Blazor site will
            // Immediately redirect from the "new" page to the "Details" page of this
            // Script - so we want to ensure they have permission to it.
            var userRoleId = await _dbContext.RoleObjectAction
                             .Where(p =>
                                    p.RbacActionId.Equals((short)RBACActionEnum.Create) &&
                                    p.RbacObjectId.Equals((short)RBACObjectEnum.Scripts) &&
                                    p.Role.RoleMembershipUser.Where(us => us.UserId.Equals(userId)).Any()
                                    )
                             .Select(p => p.RoleId)
                             .FirstAsync();

            var newScript = new EphIt.Db.Models.Script
            {
                Created          = DateTime.UtcNow,
                CreatedByUserId  = userId,
                Description      = description,
                Name             = name,
                Modified         = DateTime.UtcNow,
                ModifiedByUserId = userId
            };

            _dbContext.Add(newScript);
            await _dbContext.SaveChangesAsync();

            var roleAsyncTask = AssociateWithRoleAsync(newScript.ScriptId, userRoleId);
            var auditTask     = _auditLogger.AuditLog(AuditObject.Script, AuditAction.Create, AuditStatus.Success, newScript.ScriptId);

            await Task.WhenAll(roleAsyncTask, auditTask);

            return(newScript.ScriptId);
        }