public async Task <IActionResult> Delete([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); //return Ok(new ResultMessageModel() { IsSuccess = false, Message = "Under Construction." }); //check for dependencies - if other profile types depend on this, it cannot be deleted. var item = GetItem(model.ID); if (item.Dependencies != null && item.Dependencies.Count > 0) { _logger.LogWarning($"ProfileTypeDefinitionController|Delete|Could not delete type definition because other type definitions depend on it. Id:{model.ID}."); return(Ok(new ResultMessageModel() { IsSuccess = false, Message = "This item cannot be deleted because other type definitions depend on this item." })); } var result = await _dal.Delete(model.ID, userToken); if (result < 0) { _logger.LogWarning($"ProfileTypeDefinitionController|Delete|Could not delete profile type definition. Invalid id:{model.ID}."); return(BadRequest("Could not delete item. Invalid id.")); } _logger.LogInformation($"ProfileTypeDefinitionController|Delete|Deleted profile type definition. Id:{model.ID}."); //return success message object return(Ok(new ResultMessageModel() { IsSuccess = true, Message = "Item was deleted." })); }
public IActionResult GetLibrary([FromBody] ProfileTypeDefFilterModel model) { if (model == null) { _logger.LogWarning("ProfileTypeDefinitionController|GetLibrary|Invalid model"); return(BadRequest("ProfileTypeDefinitionController|Library|Invalid model")); } var userToken = UserExtension.DalUserToken(User); // { UserId = User.GetUserID() }; //search on some pre-determined fields var orderByExprs = _profileUtils.BuildSearchOrderByExpressions(User.GetUserID(), (SearchCriteriaSortByEnum)model.SortByEnum); var result = _dal.Where(BuildPredicate(model, userToken), userToken, model.Skip, model.Take, true, false, orderByExprs.ToArray()); //TBD - come back to this - //This is used when user clicks on View Type Defs for a single profile. //Add support to return list or profiles if the front end begins to support filtering by multiple profiles var profileCategory = model.Filters == null ? null : model.Filters.Find(c => c.ID.Value == (int)SearchCriteriaCategoryEnum.Profile); var profileFilters = profileCategory == null ? null : profileCategory.Items.Where(x => x.Selected).Select(x => x.ID.Value).ToList(); return(Ok(new ProfileTypeDefinitionSearchResult <ProfileTypeDefinitionModel>() { Count = result.Count, Data = result.Data, SummaryData = result.SummaryData, //return the profiles used in the filter -null, one or many Profiles = profileFilters == null ? null : _dalProfile.Where(x => profileFilters.Contains(x.ID.Value), userToken).Data })); }
public Task <ResultMessageWithDataModel> Export([FromBody] IdIntModel model, [FromServices] OpcUaImporter exporter) { var userToken = UserExtension.DalUserToken(User); var profileItem = _dal.GetById(model.ID, userToken); if (profileItem == null /* || !(profileItem.AuthorId == null || profileItem.AuthorId == userToken)*/) { return(Task.FromResult(new ResultMessageWithDataModel() { IsSuccess = false, Message = $"Item {model?.ID} not found", })); } // Populate the OPC model into a new importer instance string xmlNodeSet = null; using (var xmlNodeSetStream = new MemoryStream()) { if (exporter.ExportProfileItem(profileItem, xmlNodeSetStream, userToken, null)) { xmlNodeSet = Encoding.UTF8.GetString(xmlNodeSetStream.ToArray()); // TODO read and include the required models in a ZIP file, optionally? } } return(Task.FromResult(new ResultMessageWithDataModel() { IsSuccess = true, Message = "Type definition was exported.", Data = xmlNodeSet, })); }
public IActionResult ValidateModel([FromBody] ProfileModel model) { if (model == null) { _logger.LogWarning($"ProfileController|ValidateModel|Invalid model (null)"); return(BadRequest($"Invalid model (null). Check Publish Date formatting.")); } var userToken = UserExtension.DalUserToken(User); //test for unique namespace/owner id/publish date combo if (!IsValidModel(model, userToken)) { return(Ok(new ResultMessageWithDataModel() { IsSuccess = false, Message = "There is already a profile with this namespace and publish date combination. Enter a different namespace or publish date.", Data = null })); } //if we get here, passed return(Ok(new ResultMessageWithDataModel() { IsSuccess = true, Message = "Valid", Data = null })); }
public IActionResult GetByID([FromBody] IdIntModel model) { if (model == null) { _logger.LogWarning($"ProfileTypeDefinitionController|GetByID|Invalid model (null)"); return(BadRequest($"Invalid model (null)")); } var result = this.GetItem(model.ID); if (result == null) { _logger.LogWarning($"ProfileTypeDefinitionController|GetById|No records found matching this ID: {model.ID}"); return(BadRequest($"No records found matching this ID: {model.ID}")); } //increment page visit count for this item var userToken = UserExtension.DalUserToken(User); var analytic = _dalAnalytics.Where(x => x.ProfileTypeDefinitionId == model.ID, userToken, null, null, false).Data.FirstOrDefault(); if (analytic == null) { _dalAnalytics.Add(new ProfileTypeDefinitionAnalyticModel() { ProfileTypeDefinitionId = model.ID, PageVisitCount = 1 }, userToken); } else { analytic.PageVisitCount += 1; _dalAnalytics.Update(analytic, null); } return(Ok(result)); }
public async Task <IActionResult> DeleteMany([FromBody] List <IdIntModel> model) { var userToken = UserExtension.DalUserToken(User); //check that this user is author of all the items they are trying to delete var ids = model.Select(x => x.ID).ToList(); //This also deletes all associated type defs, attributes, custom data types, compositions, interfaces //associated with this nodeset and its profiles try { var result = await _dal.DeleteMany(model.Select(x => x.ID).ToList(), userToken); if (result < 0) { _logger.LogWarning($"ProfileController|DeleteMany|Could not delete item(s). Invalid model:{string.Join(", ", ids)}."); return(BadRequest("Could not delete item(s). Invalid id(s).")); } else { _logger.LogInformation($"ProfileController|Delete|Deleted nodeset item(s). Id:{string.Join(", ", model)}."); //return success message object return(Ok(new ResultMessageModel() { IsSuccess = true, Message = model.Count == 1 ? "Item was deleted." : $"{model.Count} Items were deleted." })); } } catch (Exception ex) { _logger.LogCritical(ex, ex.Message); return(BadRequest("Could not delete item(s). Invalid id(s).")); } }
public async Task <IActionResult> Update([FromBody] UserModel model) { var userToken = UserExtension.DalUserToken(User); ValidateModel(model, userToken); if (!ModelState.IsValid) { return(Ok(new ResultMessageModel() { IsSuccess = false, Message = ExtractModelStateErrors().ToString() }));; } var result = await _dal.Update(model, userToken); if (result < 0) { _logger.LogWarning($"Could not update user. Invalid id:{model.ID}."); return(Ok(new ResultMessageModel() { IsSuccess = false, Message = "Could not update user. Invalid id." }));; } _logger.LogInformation($"Updated user. Id:{model.ID}."); //return success message object return(Ok(new ResultMessageModel() { IsSuccess = true, Message = "Item was updated." })); }
public IActionResult CopyUser([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); //get existing user, wipe out certain pieces of info, add new user //update new user's password, var result = _dal.GetById(model.ID, userToken); if (result == null) { //var errors = ExtractModelStateErrors(); return(BadRequest("Original user not found.")); } //keep permissions and organization same result.ID = null; result.Created = DateTime.UtcNow; result.Email = null; result.FirstName = null; result.LastName = null; result.UserName = null; result.LastLogin = null; result.RegistrationComplete = null; return(Ok(result)); }
public async Task <IActionResult> Add([FromBody] UserModel model) { if (!ModelState.IsValid) { return(Ok(new ResultMessageModel() { IsSuccess = false, Message = ExtractModelStateErrors().ToString() }));; } var userToken = UserExtension.DalUserToken(User); var result = await _dal.Add(model, userToken); model.ID = result; if (result == 0) { _logger.LogWarning($"Could not add user: {model.FirstName} {model.LastName}."); return(Ok(new ResultMessageModel() { IsSuccess = false, Message = "Could not add user. " }));; } _logger.LogInformation($"Added user item. Id:{result}."); //return success message object return(Ok(new ResultMessageModel() { IsSuccess = true, Message = "Item was added." })); }
public IActionResult GetProfileExplorer([FromBody] IdIntModel model) { if (model == null) { _logger.LogWarning($"ProfileTypeDefinitionController|GetProfileExplorer|Invalid model (null)"); return(BadRequest($"Invalid model (null)")); } var userToken = UserExtension.DalUserToken(User); var profile = _dal.GetById(model.ID, userToken); if (profile == null) { _logger.LogWarning($"ProfileTypeDefinitionController|GetProfileExplorer|No records found matching this ID: {model.ID}"); return(BadRequest($"No records found matching this ID: {model.ID}")); } //Build the explorer... var dependencies = _profileUtils.GenerateDependencies(profile, userToken); //Build the explorer inheritance tree... var treeview = _profileUtils.GenerateAncestoryTree(profile, userToken, true); // note interfaces, compositions already accounted for in profile object var result = new ProfileExplorerModel() { Profile = profile, Dependencies = dependencies, Tree = treeview }; return(Ok(result)); }
public IActionResult Search([FromBody] PagerFilterSimpleModel model) { if (model == null) { return(BadRequest("User|Search|Invalid model")); } var userToken = UserExtension.DalUserToken(User); if (string.IsNullOrEmpty(model.Query)) { return(Ok(_dal.GetAllPaged(userToken, model.Skip, model.Take, true))); } model.Query = model.Query.ToLower(); var result = _dal.Where(s => //string query section //s.IsActive && (s.UserName.ToLower().Contains(model.Query) || (s.FirstName.ToLower() + s.LastName.ToLower()).Contains( model.Query.Replace(" ", "").Replace("-", ""))), //in case they search for code and name in one string. userToken, model.Skip, model.Take, true); return(Ok(result)); }
public async Task <IActionResult> ToggleFavorite([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); var item = _dal.GetById(model.ID, userToken); item.IsFavorite = !item.IsFavorite; return(await UpdateInternal(item, false)); }
public IActionResult LookupFavorites() { var userToken = UserExtension.DalUserToken(User); // { UserId = User.GetUserID() }; //search on some pre-determined fields var result = _dal.Where(x => x.Favorite != null && x.Favorite.IsFavorite, userToken, null, null, false, false); return(Ok(result)); }
public IActionResult LookupProfileRelatedExtend([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); var parent = _dal.GetById(model.ID, userToken); var result = new ProfileLookupModel(); result.Compositions = _profileUtils.BuildCompositionLookupExtend(parent, userToken); result.Interfaces = _profileUtils.BuildInterfaceLookup(null, userToken); return(Ok(result)); }
public IActionResult GetAll() { var userToken = UserExtension.DalUserToken(User); var result = _dal.GetAll(userToken); if (result == null) { return(BadRequest($"No records found.")); } return(Ok(result)); }
public IActionResult InitProfileTypeDefinition() { var userToken = UserExtension.DalUserToken(User); //TBD - move search condition to config file //TBD - also include opc node id as qualifier? //always extend from baseObjectType. If null, throw exception. var matches = _dal.Where(x => x.Name.ToLower().Equals("baseobjecttype"), userToken, null, null, false, true).Data; if (matches == null || matches.Count == 0) { _logger.LogWarning($"ProfileTypeDefinitionController|InitProfileTypeDefinition|BaseObjectType not found"); return(BadRequest($"BaseObjectType not found")); } else if (matches.Count > 1) { _logger.LogWarning($"ProfileTypeDefinitionController|InitProfileTypeDefinition|Multiple items with name BaseObjectType found"); return(BadRequest($"Multiple items with name BaseObjectType found")); } var result = ExtendTypeDefinitionInternal(matches[0], null, false, userToken); //reset the type id so the user can select type. result.Type = null; result.TypeId = -1; ////used to populate author info //var user = _dalUser.GetById(User.GetUserID()); //var author = new UserSimpleModel() //{ // ID = user.ID, // FirstName = user.FirstName, // LastName = user.LastName, // Organization = user.Organization //}; ////create a new empty profile with proper defaults and structure //var result = new ProfileTypeDefinitionModel() //{ // AuthorId = User.GetUserID(), // Author = author, // CreatedBy = author, // Created = DateTime.UtcNow, // UpdatedBy = author, // Updated = DateTime.UtcNow, // Dependencies = new List<ProfileTypeDefinitionSimpleModel>(), // Ancestory = new List<ProfileTypeDefinitionSimpleModel>(), // ProfileAttributes = new List<ProfileAttributeModel>(), // MetaTagsConcatenated = "", // TypeId = -1 //}; return(Ok(result)); }
public IActionResult GetByID([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); var result = _dal.GetById(model.ID, userToken); if (result == null) { return(BadRequest($"No records found matching this ID: {model.ID}")); } return(Ok(result)); }
public IActionResult GetCounts() { var userToken = UserExtension.DalUserToken(User); var all = _dal.Count(s => s.StandardProfileID.HasValue, userToken);// && (!s.AuthorId.HasValue || s.AuthorId.Value.Equals(userId))); var mine = _dal.Count(s => !s.StandardProfileID.HasValue && s.AuthorId.HasValue && s.AuthorId.Value.Equals(userToken.UserId), userToken); return(Ok(new ProfileCountModel() { All = all, Mine = mine })); }
public IActionResult GetCounts() { var userToken = UserExtension.DalUserToken(User); var all = _dal.Count(p => p.Author == null && !ProfileMapperUtil.ExcludedProfileTypes.Contains(p.ProfileTypeId), userToken); // !p.ProfileTypeId.Equals((int)ProfileItemTypeEnum.Object), userToken); var mine = _dal.Count(p => p.Author != null && p.Author.ID.Equals(userToken.UserId) && !ProfileMapperUtil.ExcludedProfileTypes.Contains(p.ProfileTypeId), userToken); // !p.ProfileTypeId.Equals((int)ProfileItemTypeEnum.Object), userToken); return(Ok(new ProfileCountModel() { All = all, Mine = mine })); }
public async Task <IActionResult> Add([FromBody] ProfileModel model) { if (model == null) { _logger.LogWarning($"ProfileController|Add|Invalid model (null)"); return(BadRequest($"Invalid model (null). Check Publish Date formatting.")); } var userToken = UserExtension.DalUserToken(User); //test for unique namespace/owner id/publish date combo if (!IsValidModel(model, userToken)) { return(Ok(new ResultMessageWithDataModel() { IsSuccess = false, Message = "There is already a profile with this namespace and publish date combination. Enter a different namespace or publish date.", Data = null })); } //set some values server side model.AuthorId = userToken.UserId; model.NodeSetFiles = null; model.StandardProfileID = null; //re-validate ModelState.Clear(); TryValidateModel(model); if (!ModelState.IsValid) { var errors = ExtractModelStateErrors(); return(BadRequest("The profile record is invalid. Please correct the following: " + errors.ToString())); } var id = await _dal.Add(model, userToken); if (id < 0) { _logger.LogWarning($"ProfileController|Add|Could not add profile item."); return(BadRequest("Could not add profile item.")); } _logger.LogInformation($"ProfileController|Add|Added profile item. Id:{id}."); return(Ok(new ResultMessageWithDataModel() { IsSuccess = true, Message = "Item was added.", Data = id })); }
public IActionResult ExtendWizard([FromBody] ProfileTypeDefinitionWizardExtendModel model) { var userToken = UserExtension.DalUserToken(User); var parent = _dal.GetById(model.ID, userToken); if (parent == null) { _logger.LogWarning($"ProfileTypeDefinitionController|ExtendWizard|No records found matching this ID: {model.ID}"); return(BadRequest($"No records found matching this ID: {model.ID}")); } var result = ExtendTypeDefinitionInternal(parent, model.ProfileId, true, userToken); return(Ok(result)); }
public IActionResult LookupProfileRelated([FromBody] IdIntModel model) { //for a new profile being created from front end, the profile won't have an id yet. if (model == null || model.ID <= 0) { return(Ok(new ProfileLookupModel())); } var userToken = UserExtension.DalUserToken(User); var profile = _dal.GetById(model.ID, userToken); var result = new ProfileLookupModel(); result.Compositions = _profileUtils.BuildCompositionLookup(profile, userToken); result.Interfaces = _profileUtils.BuildInterfaceLookup(profile, userToken); return(Ok(result)); }
public async Task <IActionResult> ImportSlow([FromBody] List <ImportOPCModel> model, [FromServices] OpcUaImporter importer) { _logger.LogInformation($"ImportLogController|Import/Slow."); var userToken = UserExtension.DalUserToken(User); var logId = await _svcImport.CallSlowMethod(model, userToken); return(Ok( new ResultMessageWithDataModel() { IsSuccess = true, Message = "Import is processing...", Data = logId } )); }
public async Task <IActionResult> Import([FromBody] List <ImportOPCModel> model /*, [FromServices] OpcUaImporter importer*/) { if (!ModelState.IsValid) { var errors = ExtractModelStateErrors(); _logger.LogCritical($"ProfileController|Import|User Id:{User.GetUserID()}, Errors: {errors.ToString()}"); return(Ok( new ResultMessageWithDataModel() { IsSuccess = false, Message = "The nodeset data is invalid." } )); } if (model == null || model.Count == 0) { _logger.LogWarning($"ProfileController|Import|No nodeset files to import. User Id:{User.GetUserID()}."); return(Ok( new ResultMessageWithDataModel() { IsSuccess = false, Message = "No nodeset files to import." } )); } _logger.LogInformation($"ProfileController|ImportMyOpcUaProfile|Importing {model.Count} nodeset files. User Id:{User.GetUserID()}."); //pass in the author id as current user var userToken = UserExtension.DalUserToken(User); //var result = await this.ImportOpcUaNodeSet(importer, model, userId, userId); //kick off background process, logid is returned immediately so front end can track progress... var logId = await _svcImport.ImportOpcUaNodeSet(model, userToken); return(Ok( new ResultMessageWithDataModel() { IsSuccess = true, Message = "Import is processing...", Data = logId } )); }
public async Task <IActionResult> Delete([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); var result = await _dal.Delete(model.ID, userToken); if (result < 0) { _logger.LogWarning($"Could not delete user. Invalid id:{model.ID}."); return(BadRequest("Could not delete user. Invalid id.")); } _logger.LogInformation($"Deleted user. Id:{model.ID}."); //return success message object return(Ok(new ResultMessageModel() { IsSuccess = true, Message = "Item was deleted." })); }
public IActionResult GetByID([FromBody] IdIntModel model) { if (model == null) { _logger.LogWarning($"ProfileController|GetByID|Invalid model (null)"); return(BadRequest($"Invalid model (null)")); } var userToken = UserExtension.DalUserToken(User); var result = _dal.GetById(model.ID, userToken); if (result == null) { _logger.LogWarning($"ProfileController|GetById|No records found matching this ID: {model.ID}"); return(BadRequest($"No records found matching this ID: {model.ID}")); } return(Ok(result)); }
public async Task <IActionResult> Delete([FromBody] IdIntModel model) { var userToken = UserExtension.DalUserToken(User); //This also deletes all associated type defs, attributes, custom data types, compositions, interfaces //associated with this profile try { var result = await _dal.Delete(model.ID, userToken); if (result <= 0) { _logger.LogWarning($"ProfileController|Delete|Could not delete item. Invalid id:{model.ID}."); return(BadRequest("Could not delete item. Invalid id.")); } _logger.LogInformation($"ProfileController|Delete|Deleted item. Id:{model.ID}."); } catch (Npgsql.PostgresException eDB) { //trap foreign key error and let user know there is a dependency issue _logger.LogCritical($"ProfileController|Delete|Id:{model.ID}.", eDB); if (eDB.Message.ToLower().Contains("violates foreign key constraint")) { return(Ok(new ResultMessageModel() { IsSuccess = false, Message = "This profile cannot be deleted because something else depends on it. Check that no other profiles nor type definitions depend on this profile or this profile's type definitions. " })); } //some other db issue return(Ok(new ResultMessageModel() { IsSuccess = false, Message = "Please contact your system administrator." })); } //return success message object return(Ok(new ResultMessageModel() { IsSuccess = true, Message = "Item was deleted." })); }
public IActionResult ExtendToken() { var userToken = UserExtension.DalUserToken(User); if (User.IsImpersonating()) { // So little tricky bit here, because we "believe" the UserID to be the org ID we cannot use the base // UserID and must acquire this from the token directly; not the helper method. var realUser = _dal.GetById(User.GetRealUserID(), userToken); // Refresh the token with the target user and org id. return(Ok(_tokenUtils.BuildImpersonationToken(realUser, User.ImpersonationTargetUserID()))); } else { var user = _dal.GetById(User.GetUserID(), userToken); var newToken = _tokenUtils.BuildToken(user); return(Ok(newToken)); } }
private ProfileTypeDefinitionModel GetItem(int id) { var userToken = UserExtension.DalUserToken(User); var result = _dal.GetById(id, userToken); if (result == null) { return(null); } //return dependencies, ancestory for this profile as part of this response to reduce volume of calls for a profile. result.Dependencies = _profileUtils.GenerateDependencies(result, userToken); result.Ancestory = _profileUtils.GenerateAncestoryLineage(result, userToken); //pull extended attributes from ancestory AND interface attributes result.ExtendedProfileAttributes = _profileUtils.GetExtendedAttributes(result, userToken); //merge profile attributes, compositions, variable types result.ProfileAttributes = _profileUtils.MergeProfileAttributes(result); //reduce size of returned object and clear out individual collections result.Attributes = null; result.Compositions = null; //result.CustomDataTypes = null; return(result); }
public async Task <IActionResult> ValidateByToken([FromBody] ChangePasswordModel model) { if (string.IsNullOrEmpty(model.NewPassword)) { return(BadRequest("Password is required.")); } var userToken = UserExtension.DalUserToken(User); var user = _dal.GetById(User.GetUserID(), userToken); if (user == null) { return(BadRequest("User was not found. Please contact support.")); } // If we get here, update the user data with new password await _dal.ChangePassword(user.ID.Value, model.OldPassword, model.NewPassword); // Why do we expect a new token here? The user is already authenticated and just changing their password? var tokenModel = _tokenUtils.BuildToken(user); return(Ok(tokenModel)); }