Пример #1
0
        public long Add(long?projectId, string name, string desc, string rule, bool isSystem)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrEmpty(rule))
            {
                throw new ArgumentNullException(nameof(rule));
            }

            using (var telemetryScope = _telemetryScopeProvider.Create <Reports>(TelemetryOperationNames.Report.Create))
            {
                try
                {
                    if (_reportRepository.Get(projectId, name).Any())
                    {
                        throw new ReportAlreadyExistsException(name);
                    }

                    if (!_reportAuthorityValidator.CanCreate(_userPrincipal.Info.Id, projectId))
                    {
                        throw new UnauthorizedAccessException();
                    }

                    var report = new Reports
                    {
                        DisplayName  = name,
                        Created      = _timeService.GetUtc(),
                        CreatedById  = _userPrincipal.Info.Id,
                        Description  = desc,
                        Modified     = _timeService.GetUtc(),
                        ModifiedById = _userPrincipal.Info.Id,
                        ProjectId    = projectId,
                        Rule         = rule,
                        IsSystem     = isSystem
                    };

                    telemetryScope.SetEntity(report);

                    _reportRepository.Insert(report);

                    _reportRepository.Save();

                    telemetryScope.WriteSuccess();

                    return(report.Id);
                }
                catch (Exception ex)
                {
                    telemetryScope.WriteException(ex);

                    throw;
                }
            }
        }
Пример #2
0
        public void ShouldValidateReportCreateAuthority(bool isHasAuthority)
        {
            const long userId = 234234;

            long?projectId = 4234;

            _userAuthorityValidator
            .Setup(_ => _.HasUserAuthorities(userId, new[] { Authorities.UI.Reports.Create }, projectId))
            .Returns(isHasAuthority);

            var result = _target.CanCreate(userId, projectId);

            result.ShouldBeEquivalentTo(isHasAuthority);
        }