Example #1
0
        public UserResponse Post()
        {
            var request = new UserRequest(this);

            var user = this.Repository.Query<SewebarKey.User>()
                .FirstOrDefault(u => u.Username == request.UserName && u.Password == request.Password);

            if (user == null)
            {
                user = request.GetUser();

                this.Repository.Add(user);
            }

            var database = request.GetDatabase(user);

            if (database != null)
            {
                user.Databases.Add(database);
            }

            this.Repository.Save(user);

            return new UserResponse(user);
        }
Example #2
0
        public DatabaseResponse Post(string username)
        {
            var request = new UserRequest(this);
            var user = this.GetSewebarUser();
            var database = request.GetDatabase(user);

            if (user == null)
            {
                var owner = request.Owner;

                if (owner != null)
                {
                    // user to be registered
                    user = new SewebarKey.User
                    {
                        Username = owner.Username,
                        Password = owner.Password
                    };

                    this.Repository.Add(user);
                }
                else
                {
                    return ThrowHttpReponseException<DatabaseResponse>(
                        "No user for database.",
                        HttpStatusCode.BadRequest);
                }
            }

            if (database != null)
            {
                user.Databases.Add(database);

                this.Repository.Save(database);

                return new DatabaseResponse(database);
            }

            return ThrowHttpReponseException<DatabaseResponse>(
                "No database to register",
                HttpStatusCode.BadRequest);
        }
Example #3
0
        public UserResponse Put()
        {
            var request = new UserRequest(this);
            var user = this.GetSewebarUser();

            if (user.Username == request.UserName)
            {
                // updating himself
                if (!string.IsNullOrEmpty(request.NewUserName))
                {
                    user.Username = request.NewUserName;
                }

                if (!string.IsNullOrEmpty(request.NewPassword))
                {
                    user.Password = request.NewPassword;
                }

                this.Repository.Save(user);

                return new UserResponse(user);
            }
            else if (this.User.IsInRole("admin"))
            {
                // updating by admin
                SewebarKey.User modified = this.Repository.Query<SewebarKey.User>()
                                    .FirstOrDefault(u => u.Username == request.UserName);

                return this.ThrowHttpReponseException<UserResponse>(
                    "This feature is not yet implemented",
                    HttpStatusCode.NotImplemented);
            }

            return this.ThrowHttpReponseException<UserResponse>(
                string.Format("User \"{0}\" not found or you are not auhtorized to modify him.", request.UserName),
                HttpStatusCode.NotFound);
        }
Example #4
0
        public DatabaseResponse Put(string username, string id)
        {
            var request = new UserRequest(this);

            if (this.User.Identity.Name == username || this.User.IsInRole("admin"))
            {
                SewebarKey.Database database = this.Repository.Query<SewebarKey.Database>()
                                    .FirstOrDefault(d => d.Name == id && d.Owner.Username == username);

                if (database != null)
                {
                    database.Password = request.DbPassword;

                    this.Repository.Save(database);

                    return new DatabaseResponse(database);
                }

                return ThrowHttpReponseException<DatabaseResponse>(
                    string.Format("Database \"{0}\" for user \"{1}\" was not found.", id, username),
                    HttpStatusCode.NotFound);
            }

            return ThrowHttpReponseException<DatabaseResponse>(
                string.Format("Database \"{0}\" was not found or you are not authorized to modify it.", id),
                HttpStatusCode.Unauthorized);
        }