public void Ini() { if (UseAzureDirectory) { var azureBlobConnectionString = config.GetValue <string>("AzureDirectoryConnectionString"); var azureDirectoryCachePath = ReplacePathTokens(config.GetValue <string>("AzureDirectoryCachePath")); var azureDirectoryContainerName = config.GetValue <string>("AzureDirectoryContainerName"); //if empty string, ensure value is null if (string.IsNullOrEmpty(azureDirectoryContainerName)) { azureDirectoryContainerName = null; } var cloudStorageAccount = CloudStorageAccount.Parse(azureBlobConnectionString); Directory = new AzureDirectory(cloudStorageAccount, azureDirectoryCachePath, containerName: azureDirectoryContainerName); } else { if (!System.IO.Directory.Exists(INDEXPATH)) { System.IO.Directory.CreateDirectory(INDEXPATH); } Directory = FSDirectory.Open(INDEXPATH); } bool create = !DirectoryReader.IndexExists(Directory); lock (write_lock) { try { if (CanWrite) { SetWriter(create); if (Writer != null && !UseAzureDirectory) { CloseWriter(); } //Writer.Optimize(); } } catch (Lucene.Net.Store.LockObtainFailedException ex) { logger.Log(ex); } catch (Exception ex) { throw; //logger.Log(ex); } finally { //CloseWriter(); } } SetSearcher(); }
public JsonResult DeleteParameters(string key) { bool success = false; string message = ""; try { var metas = repo.GetPuckMeta().Where(x => x.Name == DBNames.EditorSettings && x.Key == key).ToList(); var meta = metas.FirstOrDefault(); metas.ForEach(x => repo.DeleteMeta(x)); //clear cached values var cachePrefix = "editor_settings_"; var cacheKey = cachePrefix + key; cache.Remove(cacheKey); cache.Remove("null_" + cacheKey); var instruction = new PuckInstruction() { Count = 2, ServerName = ApiHelper.ServerName(), InstructionDetail = $"{cacheKey},{"null_" + cacheKey}", InstructionKey = InstructionKeys.RemoveFromCache }; repo.AddPuckInstruction(instruction); repo.SaveChanges(); if (meta != null) { var keyParts = key.Split(new char[] { ':' }); var typeSettings = ApiHelper.EditorSettingTypes().FirstOrDefault(x => x.FullName == keyParts[0]); object model = JsonConvert.DeserializeObject(meta.Value, typeSettings); ApiHelper.OnAfterSettingsDelete(this, new puck.core.Events.AfterEditorSettingsDeleteEventArgs { Setting = (I_Puck_Editor_Settings)model , SettingsTypeFullName = keyParts[0] , ModelTypeName = keyParts[1] , PropertyName = keyParts[2] }); } success = true; } catch (Exception ex) { success = false; message = ex.Message; log.Log(ex); } return(Json(new { success = success, message = message })); }
public async Task <PuckImage> Transform(BaseModel m, string propertyName, string ukey, PuckImage p, Dictionary <string, object> dict) { try { if (p.File == null || string.IsNullOrEmpty(p.File.FileName)) { return(p); } StorageCredentials creden = new StorageCredentials(accountName, accessKey); CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: false); CloudBlobClient client = acc.CreateCloudBlobClient(); CloudBlobContainer cont = client.GetContainerReference(containerName); if (await cont.CreateIfNotExistsAsync()) { await cont.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); } string filepath = string.Concat(m.Id, "/", m.Variant, "/", ukey, "_", p.File.FileName); CloudBlockBlob cblob = cont.GetBlockBlobReference(filepath); //var stream = new MemoryStream(); //p.File.CopyTo(stream); p.Size = p.File.Length; p.Extension = Path.GetExtension(p.File.FileName).Replace(".", ""); using (var stream = p.File.OpenReadStream()) { await cblob.UploadFromStreamAsync(stream); p.Path = $"https://{accountName}.blob.core.windows.net/{containerName}/{filepath}"; stream.Position = 0; var img = Image.Load(stream); p.Width = img.Width; p.Height = img.Height; } if (PuckCache.CropSizes != null && p.Crops != null && !string.IsNullOrEmpty(p.Path)) { p.CropUrls = new Dictionary <string, string>(); foreach (var cropSize in PuckCache.CropSizes) { p.CropUrls[cropSize.Key] = p.GetCropUrl(cropAlias: cropSize.Key); } } } catch (Exception ex) { logger.Log(ex); }finally { p.File = null; } return(p); }
public async Task <PuckImage> Transform(BaseModel m, string propertyName, string ukey, PuckImage p, Dictionary <string, object> dict) { try { if (p.File == null || string.IsNullOrEmpty(p.File.FileName)) { return(p); } string filepath = string.Concat("~/wwwroot/Media/", m.Id, "/", m.Variant, "/", ukey, "_", p.File.FileName); string absfilepath = ApiHelper.MapPath(filepath); new FileInfo(absfilepath).Directory.Create(); using (var stream = new FileStream(absfilepath, FileMode.Create)) { p.File.CopyTo(stream); } p.Path = filepath.Replace("~/wwwroot", ""); p.Size = p.File.Length; p.Extension = Path.GetExtension(p.File.FileName).Replace(".", "");; var img = Image.Load(absfilepath); p.Width = img.Width; p.Height = img.Height; }catch (Exception ex) { logger.Log(ex); } finally { p.File = null; } return(p); }
public async Task <ActionResult> Edit(string p_type, IFormCollection fc) { //var targetType = Type.GetType(p_type); var targetType = apiHelper.TaskTypes().FirstOrDefault(x => x.FullName.Equals(p_type)); var model = Activator.CreateInstance(targetType); bool success = false; string message = ""; try { if (await TryUpdateModelAsync(model, model.GetType(), "")) { var mod = model as BaseTask; PuckMeta taskMeta = null; if (mod.Id != -1) { taskMeta = repo.GetPuckMeta().Where(x => x.Id == mod.Id).FirstOrDefault(); taskMeta.Value = JsonConvert.SerializeObject(mod); } else { taskMeta = new PuckMeta(); taskMeta.Name = DBNames.Tasks; taskMeta.Key = mod.GetType().FullName; taskMeta.Value = taskMeta.Value = JsonConvert.SerializeObject(mod); repo.AddPuckMeta(taskMeta); } repo.SaveChanges(); StateHelper.UpdateTaskMappings(true); success = true; } else { success = false; message = string.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); } } catch (Exception ex) { success = false; message = ex.Message; log.Log(ex); } return(Json(new { success = success, message = message })); }
public JsonResult DeleteParameters(string key) { bool success = false; string message = ""; try { repo.GetPuckMeta().Where(x => x.Name == DBNames.EditorSettings && x.Key == key).ToList().ForEach(x => repo.DeleteMeta(x)); repo.SaveChanges(); success = true; } catch (Exception ex) { success = false; message = ex.Message; log.Log(ex); } return(Json(new { success = success, message = message })); }
public async Task <ActionResult> PreviewFromForm(IFormCollection fc, string p_type) { var targetType = ApiHelper.ConcreteType(ApiHelper.GetTypeFromName(p_type)); var model = ApiHelper.CreateInstance(targetType); BaseModel bmodel = null; try { if (await TryUpdateModelAsync(model, model.GetType(), "")) { bmodel = model as BaseModel; } else { //return preview failed view } } catch (Exception ex) { log.Log(ex); //return preview failed view } var dmode = this.GetDisplayModeId(); string templatePath = bmodel.TemplatePath; if (!string.IsNullOrEmpty(dmode)) { string dpath = templatePath.Insert(templatePath.LastIndexOf('.') + 1, dmode + "."); if (System.IO.File.Exists(ApiHelper.MapPath(dpath))) { templatePath = dpath; } } var variant = GetVariant(bmodel.Path); HttpContext.Items["variant"] = variant; //Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(threadVariant); return(View(templatePath, model)); }
public async Task <IActionResult> Notifications(int since) { var success = false; var message = ""; var count = 0; var id = 0; try { var notifications = await apiHelper.GetCurrentWorkflowItemId(User.Identity.Name, since : since); id = notifications.Item1; count = notifications.Item2; success = true; } catch (Exception ex) { message = ex.Message; log.Log(ex); } return(Json(new { success = success, message = message, id = id, count = count })); }
public async Task <JsonResult> Edit(PuckUserViewModel user, bool edit) { bool success = false; string message = ""; string startPath = "/"; Guid startNodeId = Guid.Empty; var model = new PuckUserViewModel(); try { if (!ModelState.IsValid) { throw new Exception("model invalid."); } if (!edit) { if (string.IsNullOrEmpty(user.Password)) { throw new Exception("please enter a password"); } var puser = new PuckUser { PuckFirstName = user.FirstName, PuckSurname = user.Surname, Email = user.Email, UserName = user.UserName, PuckUserVariant = user.UserVariant, PuckStartNodeId = user.StartNode?.FirstOrDefault()?.Id ?? Guid.Empty }; var result = await userManager.CreateAsync(puser, user.Password); if (!result.Succeeded) { message = string.Join(" ", result.Errors.Select(x => x.Description)); throw new Exception(message); } if (user.Roles != null && user.Roles.Count > 0) { await userManager.AddToRolesAsync(puser, user.Roles.ToArray()); } if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck)) { await userManager.AddToRoleAsync(puser, PuckRoles.Puck); } success = true; } else { var puser = await userManager.FindByEmailAsync(user.CurrentEmail); if (puser == null) { throw new Exception("could not find user for edit"); } if (!puser.Email.Equals(user.Email)) { puser.Email = user.Email; } if (!puser.UserName.Equals(user.UserName)) { puser.UserName = user.UserName; } user.Roles = user.Roles ?? new List <string>(); var roles = (await userManager.GetRolesAsync(puser)).ToList(); List <string> rolesToAdd = new List <string>(); List <string> rolesToRemove = new List <string>(); //get roles to remove foreach (var r in roles) { if (!user.Roles.Contains(r)) { rolesToRemove.Add(r); } } //get roles to add foreach (var r in user.Roles) { if (!roles.Contains(r)) { rolesToAdd.Add(r); } } rolesToRemove.RemoveAll(x => x.Equals(PuckRoles.Puck)); if (rolesToRemove.Count > 0) { await userManager.RemoveFromRolesAsync(puser, rolesToRemove.ToArray()); } if (rolesToAdd.Count > 0) { await userManager.AddToRolesAsync(puser, rolesToAdd); } if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck)) { await userManager.AddToRoleAsync(puser, PuckRoles.Puck); } if (user.StartNode == null || user.StartNode.Count == 0) { puser.PuckStartNodeId = Guid.Empty; } else { Guid picked_id = user.StartNode.First().Id; var revision = repo.GetPuckRevision().Where(x => x.Id == picked_id && x.Current).FirstOrDefault(); if (revision != null) { startPath = revision.Path + "/"; } puser.PuckStartNodeId = picked_id; } if (!string.IsNullOrEmpty(user.UserVariant)) { puser.PuckUserVariant = user.UserVariant; } puser.PuckFirstName = user.FirstName; puser.PuckSurname = user.Surname; await userManager.UpdateAsync(puser); startNodeId = puser.PuckStartNodeId.Value; if (!string.IsNullOrEmpty(user.Password)) { var token = await userManager.GeneratePasswordResetTokenAsync(puser); var result = await userManager.ResetPasswordAsync(puser, token, user.Password); if (result.Succeeded) { success = true; } else { success = false; message = string.Join(" ", result.Errors.Select(x => x.Description)); } } else { success = true; } } } catch (Exception ex) { log.Log(ex); success = false; message = ex.Message; } return(Json(new { success = success, message = message, startPath = startPath, startNodeId = startNodeId })); }
public async Task <JsonResult> Edit(PuckUserViewModel user, bool edit) { bool success = false; string message = ""; string startPaths = ""; string startNodeIds = ""; var model = new PuckUserViewModel(); try { if (!ModelState.IsValid) { throw new Exception("model invalid."); } if (string.IsNullOrEmpty(user.UserGroups)) { throw new Exception("please select at least one user group"); } if (!edit) { if (string.IsNullOrEmpty(user.Password)) { throw new Exception("please enter a password"); } var puser = new PuckUser { PuckFirstName = user.FirstName, PuckSurname = user.Surname, Email = user.Email, UserName = user.UserName, PuckUserVariant = user.UserVariant, PuckStartNodeIds = user.StartNodes == null ? null : string.Join(",", user.StartNodes.Select(x => x.Id.ToString())), PuckUserGroups = user.UserGroups }; var result = await userManager.CreateAsync(puser, user.Password); if (!result.Succeeded) { message = string.Join(" ", result.Errors.Select(x => x.Description)); throw new Exception(message); } if (user.Roles != null && user.Roles.Count > 0) { await userManager.AddToRolesAsync(puser, user.Roles.ToArray()); } if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck)) { await userManager.AddToRoleAsync(puser, PuckRoles.Puck); } success = true; } else { var puser = await userManager.FindByEmailAsync(user.CurrentEmail); if (puser == null) { throw new Exception("could not find user for edit"); } puser.PuckUserGroups = user.UserGroups; if (!puser.Email.Equals(user.Email)) { puser.Email = user.Email; } if (!puser.UserName.Equals(user.UserName)) { puser.UserName = user.UserName; } user.Roles = user.Roles ?? new List <string>(); var roles = (await userManager.GetRolesAsync(puser)).Where(x => x.StartsWith("_")).ToList(); List <string> rolesToAdd = new List <string>(); List <string> rolesToRemove = new List <string>(); //get roles to remove foreach (var r in roles) { if (!user.Roles.Contains(r)) { rolesToRemove.Add(r); } } //get roles to add foreach (var r in user.Roles) { if (!roles.Contains(r)) { rolesToAdd.Add(r); } } rolesToRemove.RemoveAll(x => x.Equals(PuckRoles.Puck)); if (rolesToRemove.Count > 0) { await userManager.RemoveFromRolesAsync(puser, rolesToRemove.ToArray()); } if (rolesToAdd.Count > 0) { await userManager.AddToRolesAsync(puser, rolesToAdd); } if (!await userManager.IsInRoleAsync(puser, PuckRoles.Puck)) { await userManager.AddToRoleAsync(puser, PuckRoles.Puck); } if (user.StartNodes == null || user.StartNodes.Count == 0) { puser.PuckStartNodeIds = null; } else { Guid[] startIds = user.StartNodes.Select(x => x.Id).ToArray(); var nodes = repo.GetPuckRevision().Where(x => startIds.Contains(x.Id) && x.Current).ToList().GroupBy(x => x.Id); if (nodes.Any()) { startPaths = string.Join(",", nodes.Select(x => x.First().Path)); startNodeIds = string.Join(",", nodes.Select(x => x.First().Id.ToString())); } puser.PuckStartNodeIds = string.Join(",", startIds.Select(x => x.ToString())); } if (!string.IsNullOrEmpty(user.UserVariant)) { puser.PuckUserVariant = user.UserVariant; } puser.PuckFirstName = user.FirstName; puser.PuckSurname = user.Surname; await userManager.UpdateAsync(puser); if (!string.IsNullOrEmpty(user.Password)) { var token = await userManager.GeneratePasswordResetTokenAsync(puser); var result = await userManager.ResetPasswordAsync(puser, token, user.Password); if (result.Succeeded) { success = true; } else { success = false; message = string.Join(" ", result.Errors.Select(x => x.Description)); } } else { success = true; } cache.Set <bool?>($"renewPuckClaims{user.UserName}", true); } } catch (Exception ex) { log.Log(ex); success = false; message = ex.Message; } return(Json(new { success = success, message = message, startPaths = startPaths, startNodeIds = startNodeIds })); }