public IHttpActionResult PutUser(UserBTS userBTS)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var id          = User.Identity.GetUserId();
                var tmpidRole   = db.Users.FirstOrDefault(u => u.Id == id).Roles.FirstOrDefault().RoleId;
                var tmpnameRole = db.Roles.FirstOrDefault(u => u.Id == tmpidRole).Name;
                var user        = db.Users.Find(id);
                if (user.Email != userBTS.Email && db.Users.FirstOrDefault(u => u.Email == userBTS.Email) != null)
                {
                    return(BadRequest());
                }
                if (userBTS.ChucVu != tmpnameRole)
                {
                    return(BadRequest());
                }
                user.Email     = userBTS.Email;
                user.UserName  = userBTS.Email;
                userBTS.IDUser = id;
                db.Entry <UserBTS>(userBTS).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult GetUser(string id)
        {
            UserBTS user = db.UserBTSs.Find(id);

            if (user == null)
            {
                return(NotFound());
            }
            return(Ok(db.UserBTSs.Find(id)));
        }
Example #3
0
        public IHttpActionResult PostImage()
        {
            try
            {
                var httpRequest = HttpContext.Current.Request;

                foreach (string file in httpRequest.Files)
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);

                    var postedFile = httpRequest.Files[file];
                    var ex         = RandomString(10);

                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        int MaxContentLength = 1024 * 1024 * 10; //Size = 10 MB

                        IList <string> AllowedFileExtensions = new List <string> {
                            ".jpg", ".gif", ".png"
                        };
                        var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                        var extension = ext.ToLower();
                        if (!AllowedFileExtensions.Contains(extension))
                        {
                            return(StatusCode(HttpStatusCode.BadRequest));
                        }
                        else if (postedFile.ContentLength > MaxContentLength)
                        {
                            return(StatusCode(HttpStatusCode.BadRequest));
                        }
                        else
                        {
                            var filePath = HttpContext.Current.Server.MapPath("~/image/" + ex + postedFile.FileName);
                            postedFile.SaveAs(filePath);
                        }
                    }
                    var     id   = User.Identity.GetUserId();
                    UserBTS user = db.UserBTSs.Find(id);
                    user.Image = ex + postedFile.FileName;
                    db.SaveChanges();
                    return(Ok(user));
                    //return CreatedAtRoute("PostImage", new { id = user.IDUser }, user);
                }
                return(StatusCode(HttpStatusCode.NotFound));
            }
            catch (Exception ex)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }
        }
Example #4
0
        public IHttpActionResult GetUser(string id)
        {
            UserBTS user = db.UserBTSs.Find(id);

            if (user == null)
            {
                return(NotFound());
            }
            if (!isAdmin() && user.IDUser != User.Identity.GetUserId())
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }
            return(Ok(user));
        }
Example #5
0
        public async Task <IHttpActionResult> DeleteUserBTS(string id)
        {
            UserBTS userBTS = db.UserBTSs.Find(id);

            if (userBTS == null)
            {
                return(NotFound());
            }
            ApplicationUser user = await UserManager.FindByIdAsync(id);

            UserManager.Delete(user);
            db.UserBTSs.Remove(userBTS);
            db.SaveChanges();
            return(Ok(userBTS));
        }
Example #6
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var user = new ApplicationUser()
            {
                UserName    = model.Email,
                Email       = model.Email,
                PhoneNumber = model.Phone
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            result = UserManager.AddToRole(user.Id, model.ChucVu);
            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            UserBTS userBTS = new UserBTS
            {
                IDUser   = user.Id,
                Ten      = model.Ten,
                DiaChi   = model.DiaChi,
                GioiTinh = model.GioiTinh,
                Image    = model.Image,
                NgaySinh = model.NgaySinh,
                Email    = model.Email,
                Phone    = model.Phone,
                ChucVu   = model.ChucVu
            };

            db.UserBTSs.Add(userBTS);
            db.SaveChanges();
            return(Ok());
        }
Example #7
0
        public IHttpActionResult PutUser(UserBTS userBTS)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                //var id = User.Identity.GetUserId();
                //var tmpidRole = db.Users.FirstOrDefault(u => u.Id == id).Roles.FirstOrDefault().RoleId;
                //var tmpnameRole = db.Roles.FirstOrDefault(u => u.Id == tmpidRole).Name;
                //var user=db.Users.Find(id);
                //if (id!=userBTS.IDUser||user.Email!= userBTS.Email||tmpnameRole!=userBTS.ChucVu)
                //{
                //    return BadRequest();
                //}
                //UserBTS
                var     id   = User.Identity.GetUserId();
                UserBTS user = db.UserBTSs.Find(userBTS.IDUser);
                if (userBTS.IDUser != id || userBTS.Email != user.Email || userBTS.ChucVu != user.ChucVu)
                {
                    return(BadRequest());
                }
                user.Ten      = userBTS.Ten;
                user.DiaChi   = userBTS.DiaChi;
                user.NgaySinh = userBTS.NgaySinh;
                user.GioiTinh = userBTS.GioiTinh;
                user.Phone    = userBTS.Phone;
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }