private void CreateIdentity(string sessionJson)
        {
            var session = _apiProvider.DeserializeJson <Session>(sessionJson);
            var appUser = new AppUser(session.UserId.ToString(), "User");
            var task    = _appUserManager.CreateIdentityAsync(appUser, DefaultAuthenticationTypes.ApplicationCookie);

            task.Wait();
            var claim = task.Result;

            var authMgr        = HttpContext.GetOwinContext().Authentication;
            var authProperties = new AuthenticationProperties()
            {
                IsPersistent = false
            };

            authProperties.Dictionary["Session"] = sessionJson;

            foreach (var role in session.Roles)
            {
                var appRole = _appRoleManager.FindById(role.RoleID.ToString());
                if (appRole != null)
                {
                    _appUserManager.AddToRoleAsync(appUser.Id.ToString(), appRole.Name);
                }
            }

            authMgr.SignIn(authProperties, claim);
        }
 public HttpResponseMessage Update(HttpRequestMessage request, ApplicationRoleViewModel applicationRoleViewModel)
 {
     if (ModelState.IsValid)
     {
         var identity = (ClaimsIdentity)User.Identity;
         IEnumerable<Claim> claims = identity.Claims;
         var appRole = AppRoleManager.FindById(applicationRoleViewModel.Id);
         try
         {
             appRole.UpdateApplicationRole(applicationRoleViewModel, "update");
             AppRoleManager.Update(appRole);
             Log log = new Log()
             {
                 AppUserId = claims.FirstOrDefault().Value,
                 Content = Notification.UPDATE_ROLE,
                 Created = DateTime.Now
             };
             _logService.Create(log);
             _logService.Save();
             return request.CreateResponse(HttpStatusCode.OK, appRole);
         }
         catch (NameDuplicatedException dex)
         {
             return request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message);
         }
     }
     else
     {
         return request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
 }
        public IHttpActionResult Delete(string id)
        {
            var appRole = AppRoleManager.FindById(id);

            AppRoleManager.Delete(appRole);
            return(Ok(id));
        }
        public HttpResponseMessage Delete(HttpRequestMessage request, string id)
        {
            var appRole = AppRoleManager.FindById(id);

            AppRoleManager.Delete(appRole);
            return(request.CreateResponse(HttpStatusCode.OK, id));
        }
 public HttpResponseMessage Details(HttpRequestMessage request, string id)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         AppRole roleDb = AppRoleManager.FindById(id);
         ApplicationRoleViewModel roleVm = Mapper.Map <ApplicationRoleViewModel>(roleDb);
         response = request.CreateResponse(HttpStatusCode.OK, roleVm);
         return response;
     }));
 }
 public HttpResponseMessage Details(HttpRequestMessage request, string id)
 {
     if (string.IsNullOrEmpty(id))
     {
         return request.CreateErrorResponse(HttpStatusCode.BadRequest, nameof(id) + " không có giá trị.");
     }
     AppRole appRole = AppRoleManager.FindById(id);
     if (appRole == null)
     {
         return request.CreateErrorResponse(HttpStatusCode.NoContent, "No group");
     }
     return request.CreateResponse(HttpStatusCode.OK, appRole);
 }
        public IHttpActionResult Details(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(BadRequest());
            }
            AspNetRole appRole = AppRoleManager.FindById(id);

            if (appRole == null)
            {
                return(Ok(appRole));
            }
            return(Ok(appRole));
        }
Exemple #8
0
        public HttpResponseMessage Delete(HttpRequestMessage request, string id)
        {
            var appRole = AppRoleManager.FindById(id);

            try
            {
                AppRoleManager.Delete(appRole);
                return(request.CreateResponse(HttpStatusCode.OK, id));
            }
            catch (Exception ex)
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
Exemple #9
0
 public HttpResponseMessage Delete(HttpRequestMessage request, string id)
 {
     return(CreateHttpResponse(request, () =>
     {
         if (string.IsNullOrEmpty(id))
         {
             return request.CreateErrorResponse(HttpStatusCode.BadRequest, nameof(id) + " không có giá trị");
         }
         var model = AppRoleManager.FindById(id);
         AppRoleManager.Delete(model);
         var response = request.CreateResponse(HttpStatusCode.OK, id);
         return response;
     }));
 }
 public HttpResponseMessage Delete(HttpRequestMessage request, string id)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         _permissionService.DeleteAllByRoleID(id);
         _permissionService.SaveChange();
         _userRoleService.Delete(id);
         _userRoleService.SaveChange();
         AppRole role = AppRoleManager.FindById(id);
         AppRoleManager.Delete(role);
         response = request.CreateResponse(HttpStatusCode.OK, id);
         return response;
     }));
 }
Exemple #11
0
        public ActionResult Delete(long roleId)
        {
            AppRole        role   = roleManager.FindById(roleId);
            IdentityResult result = roleManager.Delete(role);

            if (result.Succeeded)
            {
                TempData["message"] = "역할이 성공적으로 삭제되었습니다.";
                return(RedirectToAction("Index"));
            }
            else
            {
                AddErrorsFromResult(result);
            }
            return(View());
        }
 public HttpResponseMessage Delete(HttpRequestMessage request, string id)
 {
     var appRole = AppRoleManager.FindById(id);
     var identity = (ClaimsIdentity)User.Identity;
     IEnumerable<Claim> claims = identity.Claims;
     AppRoleManager.Delete(appRole);
     Log log = new Log()
     {
         AppUserId = claims.FirstOrDefault().Value,
         Content = Notification.UPDATE_ROLE,
         Created = DateTime.Now
     };
     _logService.Create(log);
     _logService.Save();
     return request.CreateResponse(HttpStatusCode.OK, id);
 }
        public HttpResponseMessage Update(HttpRequestMessage request, ApplicationRoleViewModel applicationRoleViewModel)
        {
            HttpResponseMessage response = null;

            return(CreateHttpResponse(request, () =>
            {
                if (ModelState.IsValid)
                {
                    AppRole roleDb = AppRoleManager.FindById(applicationRoleViewModel.Id);
                    roleDb.UpdateApplicationRole(applicationRoleViewModel, "update");
                    AppRoleManager.Update(roleDb);
                    response = request.CreateResponse(HttpStatusCode.Created, roleDb);
                }
                return response;
            }));
        }
Exemple #14
0
        public HttpResponseMessage Details(HttpRequestMessage request, string id)
        {
            return(CreateHttpResponse(request, () =>
            {
                if (string.IsNullOrEmpty(id))
                {
                    return request.CreateErrorResponse(HttpStatusCode.BadRequest, nameof(id) + " không có giá trị");
                }
                var model = AppRoleManager.FindById(id);

                var responseData = Mapper.Map <AppRole, ApplicationRoleViewModel>(model);

                var response = request.CreateResponse(HttpStatusCode.OK, responseData);

                return response;
            }));
        }
Exemple #15
0
 public HttpResponseMessage Update(HttpRequestMessage request, ApplicationRoleViewModel applicationRoleViewModel)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
         }
         else
         {
             var AppRole = AppRoleManager.FindById(applicationRoleViewModel.Id);
             AppRole.UpdateApplicationRole(applicationRoleViewModel, "update");
             AppRoleManager.Update(AppRole);
             response = request.CreateResponse(HttpStatusCode.OK, applicationRoleViewModel);
         }
         return response;
     }));
 }
 public IHttpActionResult Update(ApplicationRoleViewModel applicationRoleViewModel)
 {
     if (ModelState.IsValid)
     {
         var appRole = AppRoleManager.FindById(applicationRoleViewModel.Id);
         try
         {
             appRole.UpdateApplicationRole(applicationRoleViewModel, "update");
             AppRoleManager.Update(appRole);
             return(Ok(appRole));
         }
         catch (NameDuplicatedException)
         {
             return(BadRequest());
         }
     }
     else
     {
         return(BadRequest());
     }
 }
 public HttpResponseMessage Update(HttpRequestMessage request, ApplicationRoleViewModel applicationRoleViewModel)
 {
     if (ModelState.IsValid)
     {
         var appRole = AppRoleManager.FindById(applicationRoleViewModel.Id);
         try
         {
             appRole.UpdateApplicationRole(applicationRoleViewModel, "update");
             AppRoleManager.Update(appRole);
             return(request.CreateResponse(HttpStatusCode.OK, appRole));
         }
         catch (NameDuplicatedException dex)
         {
             return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
         }
     }
     else
     {
         return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
 }
Exemple #18
0
        public IHttpActionResult PutApplicationUser(string id, UserHttpResponse applicationUser)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != applicationUser.Id)
            {
                return(BadRequest());
            }

            var user = AppUserManager.FindById(applicationUser.Id);

            if (user == null)
            {
                return(BadRequest("User Not Found"));
            }

            user.FirstName = applicationUser.FirstName;
            user.LastName  = applicationUser.LastName;
            user.Email     = applicationUser.Email;
            user.UserName  = applicationUser.Email;
            user.Timezone  = applicationUser.Timezone;
            user.CVLink    = applicationUser.CVLink;
            user.Webex     = applicationUser.Webex;
            AppUserManager.Update(user);

            foreach (var role in user.Roles.ToList())
            {
                if (!applicationUser.RoleIds.Any(i => i == role.RoleId))
                {
                    AppUserManager.RemoveFromRole(user.Id, AppRoleManager.FindById(role.RoleId).Name);
                }
            }

            foreach (var roleId in applicationUser.RoleIds)
            {
                if (!user.Roles.Any(i => i.RoleId == roleId))
                {
                    AppUserManager.AddToRole(user.Id, AppRoleManager.FindById(roleId).Name);
                }
            }

            using (var db = AppDb.Database.BeginTransaction())
            {
                try
                {
                    if (!String.IsNullOrEmpty(applicationUser.Password))
                    {
                        var result1 = AppUserManager.RemovePassword(user.Id);
                        if (!result1.Succeeded)
                        {
                            return(GetErrorResult(result1));
                        }
                        var result2 = AppUserManager.AddPassword(user.Id, applicationUser.Password);
                        if (!result2.Succeeded)
                        {
                            return(GetErrorResult(result2));
                        }
                    }
                    db.Commit();
                }
                catch (Exception)
                {
                    db.Rollback();
                }
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }
 public ApplicationRole FindById(int roleId)
 {
     return(_roleManager.FindById(roleId));
 }