コード例 #1
0
        public DatabaseResponse Post(UserRequest request, string username)
        {
            var user = this.GetLMConnectUser();
            var database = request.GetDatabase(user);

            if (user == null)
            {
                var owner = NotRegisteredUser.FromRequest(this.Request.Content.Headers);

                if (owner != null)
                {
                    // user to be registered
                    user = new LMConnect.Key.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);
        }
コード例 #2
0
        public UserResponse Post(UserRequest request)
        {
            var user = this.Repository.Query<LMConnect.Key.User>()
                .FirstOrDefault(u => u.Username == request.name && 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);
        }
コード例 #3
0
        public DatabaseResponse Put(UserRequest request, string username, string id)
        {
            if (this.User.Identity.Name == username || this.User.IsInRole("admin"))
            {
                LMConnect.Key.Database database = this.Repository.Query<LMConnect.Key.Database>()
                                    .FirstOrDefault(d => d.Name == id && d.Owner.Username == username);

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

                    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);
        }
コード例 #4
0
        public UserResponse Put(UserRequest request)
        {
            var user = this.GetLMConnectUser();

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

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

                this.Repository.Save(user);

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

                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.name),
                HttpStatusCode.NotFound);
        }