示例#1
0
        public HttpResponseMessage Register(RegisterRequest model)
        {
            Principal principal = new Principal()
            {
                Id    = Guid.NewGuid(),
                Email = model.Email,
                Name  = model.Username,
                Flags = new HashSet <PrincipalFlag> {
                    PrincipalFlag.NeedsVerification
                },
                Kind     = PrincipalKind.User,
                Password = Guid.NewGuid().ToString("N"),
                Created  = Context.Clock.UtcNow.UtcDateTime,
                Metadata = new Dictionary <string, object>()
                {
                    { "registerIp", Request.GetOwinContext().Request.RemoteIpAddress },
                    { "registerUa", Context.Client.ToString() }
                }
            };

            try
            {
                _router.Command(new CreateCommand <Principal>()
                {
                    Object = principal
                });
            }
            catch (StorageException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.Conflict));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        public HttpResponseMessage CreateIdentity([FromBody] Identity model, [FromHeader(Name = "opensheets-bypass-level")] Level bypassLevel = Level.Information)
        {
            if (model.PrincipalId != Context.Principal.Id && !Context.Identity.Flags.Contains(IdentityFlag.SysAdmin))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            ValidateResponse validateResp = _router.Query <ValidateRequest <Identity>, ValidateResponse>(
                new ValidateRequest <Identity>()
            {
                ObjectId = Guid.Empty,
                Object   = model
            });

            if (validateResp.Results.Any(x => x.Level > Level.Information))
            {
                return(Request.CreateResponse((HttpStatusCode)422, new { Validation = new { Errors = validateResp.Results } }));
            }

            model.Id = Guid.NewGuid();

            _router.Command(new CreateCommand <Identity>()
            {
                Object = model
            });

            return(Request.CreateResponse(HttpStatusCode.OK, new { Id = model.Id }));
        }
        public HttpResponseMessage CreateFile(Guid userId, Guid directoryId, Core.File fileData)
        {
            if (fileData.DirectoryId == Guid.Empty)
            {
                fileData.DirectoryId = directoryId;
            }

            CheckPermissionResponse permissionResponse = _router.Query <CheckPermissionRequest, CheckPermissionResponse>(new CheckPermissionRequest()
            {
                IdentityId = Context.Identity.Id,
                OwnerId    = userId,
                FileId     = directoryId
            });

            bool canWrite = false;

            if (!permissionResponse.EffectivePermissions.TryGetValue(FilePermissionAction.Write, out canWrite) || !canWrite)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            fileData.Id = Guid.NewGuid();

            ValidateResponse validateResp = _router.Query <ValidateRequest <Core.File>, ValidateResponse>(new ValidateRequest <Core.File>()
            {
                ObjectId = fileData.Id,
                Object   = fileData
            });

            if (validateResp.Results.Any(x => x.Level > Level.Information))
            {
                return(Request.CreateResponse((HttpStatusCode)422, new { Validation = new { Errors = validateResp.Results } }));
            }

            _router.Command(new CreateCommand <Core.File>()
            {
                Object = fileData
            });

            return(Request.CreateResponse(HttpStatusCode.OK, new { FileId = fileData.Id }));
        }