Exemplo n.º 1
0
        public IActionResult CreateProject([FromBody] CreateProjectRequest createProjectRequest)
        {
            if (!HttpContext.Session.IsAuthenticated())
            {
                return(Unauthorized());
            }

            var userId = HttpContext.Session.GetCurrentUserId();

            if (userId == null)
            {
                return(Unauthorized());
            }
            try
            {
                var user    = _userRegistry.Find((int)userId);
                var project = _projectsRegistry.Create(createProjectRequest.Name, createProjectRequest.HourValue, user);
                return(Ok(_projectsRegistry.ConvertToDetails(project)));
            }
            catch (ArgumentException e)
            {
                return(BadRequest(new SimpleMessageResponse(e.Message)));
            }
            catch (UserNotExistsException)
            {
                return(Forbid());
            }
        }
Exemplo n.º 2
0
 public void Test()
 {
     var users = new UserRegistry(new User[] {new User(new OctetString("test"), DefaultPrivacyProvider.DefaultPair)});
     Assert.AreEqual(1, users.Count);
     Assert.IsNotNull(users.Find(new OctetString("test")));
     Assert.AreEqual(1, users.Add(null).Count);
     Assert.AreEqual(2, users.Add(new User(new OctetString("test2"), DefaultPrivacyProvider.DefaultPair)).Count);
     Assert.AreEqual(2, users.Add(new User(new OctetString("test2"), DefaultPrivacyProvider.DefaultPair)).Count);
     Assert.Throws<ArgumentNullException>(() => users.Find(null));
     Assert.AreEqual("User registry: count: 2", users.ToString());
 }
        public void Test()
        {
            var users = new UserRegistry(new User[] { new User(new OctetString("test"), DefaultPrivacyProvider.DefaultPair) });

            Assert.AreEqual(1, users.Count);
            Assert.IsNotNull(users.Find(new OctetString("test")));
            Assert.AreEqual(1, users.Add(null).Count);
            Assert.AreEqual(2, users.Add(new User(new OctetString("test2"), DefaultPrivacyProvider.DefaultPair)).Count);
            Assert.AreEqual(2, users.Add(new User(new OctetString("test2"), DefaultPrivacyProvider.DefaultPair)).Count);
            Assert.Throws <ArgumentNullException>(() => users.Find(null));
            Assert.AreEqual("User registry: count: 2", users.ToString());
        }
Exemplo n.º 4
0
        public IActionResult GetUserData(int userId)
        {
            if (!HttpContext.Session.IsAuthenticated())
            {
                return(Unauthorized());
            }

            try
            {
                var currentUserData = _userRegistry.Find((int)userId);
                var userInfo        = new UserInfoResponse
                {
                    Id       = currentUserData.Id,
                    Name     = currentUserData.Name,
                    Projects = currentUserData.Projects
                               .ConvertAll(p => _projectsRegistry.Find(p.ProjectId))
                               .ConvertAll(_projectsRegistry.ConvertToDetails)
                };
                return(Ok(userInfo));
            }
            catch (Exception e) when(e is UserNotExistsException)
            {
                return(Forbid());
            }
        }
Exemplo n.º 5
0
        public IActionResult UpdateIssue(int issueId, [FromBody] CreateIssueRequest request)
        {
            if (!HttpContext.Session.IsAuthenticated())
            {
                return(Unauthorized());
            }
            var userId = HttpContext.Session.GetCurrentUserId();

            if (userId == null)
            {
                return(Unauthorized());
            }
            try
            {
                var assignee = _userRegistry.Find(request.Assignee);
                var issue    = _issueRegistry.Update(
                    issueId,
                    request.Name,
                    request.Description,
                    request.EstimateHours,
                    request.Type,
                    request.Status,
                    assignee,
                    request.Parent
                    );
                return(Ok(_issueRegistry.ConvertToDetails(issue)));
            }
            catch (ArgumentException e)
            {
                return(BadRequest(new SimpleMessageResponse(e.Message)));
            }
            catch (Exception e) when(e is UserNotExistsException || e is ProjectNotExistsException || e is IssueNotFoundException)
            {
                return(Forbid());
            }
        }
Exemplo n.º 6
0
        private static ISnmpMessage ParseMessage(int first, Stream stream, UserRegistry registry)
        {
            var array = DataFactory.CreateSnmpData(first, stream);

            if (array == null)
            {
                return(null);
            }

            if (array.TypeCode != SnmpType.Sequence)
            {
                throw new SnmpException("not an SNMP message");
            }

            var body = (Sequence)array;

            if (body.Length != 3 && body.Length != 4)
            {
                throw new SnmpException("not an SNMP message");
            }

            var                version = (VersionCode)((Integer32)body[0]).ToInt32();
            Header             header;
            SecurityParameters parameters;
            IPrivacyProvider   privacy;
            Scope              scope;

            if (body.Length == 3)
            {
                header     = Header.Empty;
                parameters = SecurityParameters.Create((OctetString)body[1]);
                privacy    = DefaultPrivacyProvider.DefaultPair;
                scope      = new Scope((ISnmpPdu)body[2]);
            }
            else
            {
                header     = new Header(body[1]);
                parameters = new SecurityParameters((OctetString)body[2]);
                privacy    = registry.Find(parameters.UserName);
                if (privacy == null)
                {
                    // handle decryption exception.
                    return(new MalformedMessage(header.MessageId, parameters.UserName, body[3]));
                }

                var code = body[3].TypeCode;
                if (code == SnmpType.Sequence)
                {
                    // v3 not encrypted
                    scope = new Scope((Sequence)body[3]);
                }
                else if (code == SnmpType.OctetString)
                {
                    // v3 encrypted
                    try
                    {
                        scope = new Scope((Sequence)privacy.Decrypt(body[3], parameters));
                    }
                    catch (DecryptionException)
                    {
                        // handle decryption exception.
                        return(new MalformedMessage(header.MessageId, parameters.UserName, body[3]));
                    }
                }
                else
                {
                    throw new SnmpException(string.Format(CultureInfo.InvariantCulture, "invalid v3 packets scoped data: {0}", code));
                }

                if (!privacy.AuthenticationProvider.VerifyHash(version, header, parameters, body[3], privacy, body.GetLengthBytes()))
                {
                    parameters.IsInvalid = true;
                }
            }

            var scopeCode = scope.Pdu.TypeCode;

            try
            {
                switch (scopeCode)
                {
                case SnmpType.TrapV1Pdu:
                    return(new TrapV1Message(body));

                case SnmpType.TrapV2Pdu:
                    return(new TrapV2Message(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                case SnmpType.GetRequestPdu:
                    return(new GetRequestMessage(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                case SnmpType.ResponsePdu:
                    return(new ResponseMessage(version, header, parameters, scope, privacy, false, body.GetLengthBytes()));

                case SnmpType.SetRequestPdu:
                    return(new SetRequestMessage(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                case SnmpType.GetNextRequestPdu:
                    return(new GetNextRequestMessage(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                case SnmpType.GetBulkRequestPdu:
                    return(new GetBulkRequestMessage(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                case SnmpType.ReportPdu:
                    return(new ReportMessage(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                case SnmpType.InformRequestPdu:
                    return(new InformRequestMessage(version, header, parameters, scope, privacy, body.GetLengthBytes()));

                default:
                    throw new SnmpException(string.Format(CultureInfo.InvariantCulture, "unsupported pdu: {0}", scopeCode));
                }
            }
            catch (Exception ex)
            {
                if (ex is SnmpException)
                {
                    throw;
                }

                throw new SnmpException("message construction exception", ex);
            }
        }