public void Migrate(MigrateRequest request) { var user = Thread.CurrentPrincipal?.Identity?.Name; log.Info($"{nameof(UserPreference)}[{user}] Starting user migration ..."); if (user == request.OldPrincipalName) { log.Warn($"{nameof(UserPreference)}[{user}] Migration action is not required for passed parameters"); throw new KeyNotFoundException(); } if (string.IsNullOrEmpty(request.NewPrincipalName) || string.IsNullOrEmpty(request.OldPrincipalName) || !request.AppCodes.Any()) { log.Warn($"{nameof(UserPreference)}[{user}] Invalid parameters passed for migration"); throw new NotSupportedException(); } try { request.NewPrincipalName = user; log.Info($"{nameof(UserPreference)}[{user}] Migrating {request.OldPrincipalName} to {request.NewPrincipalName}"); context.MigrateAccount(request); log.Info($"{nameof(UserPreference)}[{user}] Migration performed successfully"); } catch (Exception e) { log.Error($"Error on account migration: ", e); throw; } }
private DataTable BuildAppCodesParameter(MigrateRequest request) { DataTable appCodes = new DataTable(); appCodes.Columns.Add("AppCode", typeof(String)); foreach (string item in request.AppCodes) { var workRow = appCodes.NewRow(); workRow["AppCode"] = item.Trim(); appCodes.Rows.Add(workRow); } return(appCodes); }
public void MigrateAccount(MigrateRequest request) { var appCodes = BuildAppCodesParameter(request); SqlParameter oldUserNameParam = new SqlParameter("@oldUserName", request.OldPrincipalName); SqlParameter newUserNameParam = new SqlParameter("@newUserName", request.NewPrincipalName); SqlParameter appCodesParam = new SqlParameter("@appCodes", appCodes) { SqlDbType = SqlDbType.Structured, TypeName = "Core.AppCodes" }; this.Database .ExecuteSqlCommand("EXEC [Core].[usp_MigrateUserAccount] @oldUserName, @newUserName, @appCodes", oldUserNameParam, newUserNameParam, appCodesParam); }
public IHttpActionResult Migrate([FromBody] MigrateRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { service.Migrate(request); return(Ok()); } catch (UnauthorizedAccessException) { return(StatusCode(HttpStatusCode.Forbidden)); } catch (KeyNotFoundException) { return(StatusCode(HttpStatusCode.NoContent)); } catch (Exception e) { return(InternalServerError(e)); } }