public static void SharedCN(List <string> cnlist, string filePath, string filename, string keyword) { foreach (string cnPerson in cnlist) { string staffno = UserUtil.GetCnUserStaffNo(cnPerson); string id = AdUtil.GetUserIdByUsername(GlobalService.User, "kmhk.local"); string directory = @"\\kdthk-dm1\project\KDTHK-DM\littlecloud\"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string nfilepath = filePath.Replace("''", "'"); string newFileName = Path.GetFileName(nfilepath); string newFilePath = directory + newFileName; if (!File.Exists(newFilePath)) { File.Copy(nfilepath, newFilePath, true); } if (newFilePath.Contains("'")) { newFilePath = newFilePath.Replace("'", "''"); } string query = string.Format("if not exists (select * from S_OUT_SHARE where o_pathnew = N'{8}' and o_from = N'{4}' and o_toid = '{5}') " + "insert into S_OUT_SHARE (o_path, o_filename, o_keyword, o_fromid, o_from, o_toid, o_to, o_date, o_pathnew) values (N'{0}', N'{1}', N'{2}', '{3}', N'{4}', '{5}', N'{6}', '{7}', N'{8}')", filePath, filename, keyword, id, GlobalService.User, staffno, cnPerson, DateTime.Today.ToString("yyyy/MM/dd"), newFilePath); //Debug.WriteLine("Query: " + query); //DataService.GetInstance().ExecuteNonQuery(query); DataServiceMes.GetInstance().ExecuteNonQuery(query); UpdateFilePath(filePath, newFilePath); } }
private void btnSave_Click(object sender, EventArgs e) { List <string> queryList = new List <string>(); foreach (DataGridViewRow row in dgvShareSetup.Rows) { string name = row.Cells[0].Value.ToString(); string path = row.Cells[1].Value.ToString(); string folder = row.Cells[2].Value.ToString(); string shared = row.Cells[3].Value.ToString(); string keyword = row.Cells[4].Value.ToString(); string modified = row.Cells[5].Value.ToString(); string disc = row.Cells[6].Value.ToString(); string oShared = row.Cells[7].Value.ToString(); string extension = Path.GetExtension(path); string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); if (keyword == "") { keyword = name; } //if (shared == oShared) // continue; FileInfo info = new FileInfo(path); FileSecurity fs = info.GetAccessControl(); AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(NTAccount)); foreach (FileSystemAccessRule rule in rules) { fs.RemoveAccessRuleSpecific(rule); } fs.SetAccessRuleProtection(true, false); fs.AddAccessRule(new FileSystemAccessRule(@"kmhk\itadmin", FileSystemRights.FullControl, AccessControlType.Allow)); fs.AddAccessRule(new FileSystemAccessRule(AdUtil.GetUserIdByUsername(GlobalService.User, "kmhk.local"), FileSystemRights.FullControl, AccessControlType.Allow)); string sPath = path.Contains("'") ? path.Replace("'", "''") : path; if (DataUtil.IsRecordExists(path)) { List <string> list = DataUtil.GetSharedList(GlobalService.DbTable, path); if (list.Count > 0) { foreach (string item in list) { if (!UserUtil.IsCnMember(item.Trim()) && !UserUtil.IsVnMember(item.Trim()) && !UserUtil.IsJpMember(item.Trim())) { string tbName = "TB_" + AdUtil.GetUserIdByUsername(item.Trim(), "kmhk.local"); string delText = string.Format("delete from " + tbName + " where r_path = N'{0}' and r_owner = N'{1}'", sPath, GlobalService.User); DataService.GetInstance().ExecuteNonQuery(delText); } else { string delText = string.Format("delete from S_OUT_SHARE where o_path = N'{0}' and o_to = N'{1}'", sPath, item.Trim()); DataServiceMes.GetInstance().ExecuteNonQuery(delText); } } } } List <string> sharedList = shared.Split(';').ToList(); List <string> hklist = new List <string>(); List <string> cnlist = new List <string>(); List <string> vnlist = new List <string>(); List <string> jplist = new List <string>(); foreach (string item in sharedList) { if (UserUtil.IsCnMember(item.Trim())) { cnlist.Add(item.Trim()); } else if (UserUtil.IsVnMember(item.Trim())) { vnlist.Add(item.Trim()); } else if (UserUtil.IsJpMember(item.Trim())) { jplist.Add(item.Trim()); } else { hklist.Add(item.Trim()); } } if (name.Contains("'")) { name = name.Replace("'", "''"); } if (keyword.Contains("'")) { keyword = keyword.Replace("'", "''"); } if (shared != "-") { foreach (string item in hklist) { string sharedId = AdUtil.GetUserIdByUsername(item.Trim(), "kmhk.local"); string tableName = "TB_" + sharedId; string sharedDivision = SystemUtil.GetDivision(item.Trim()); string sharedDepartment = SystemUtil.GetDepartment(item.Trim()); fs.AddAccessRule(new FileSystemAccessRule(sharedId, FileSystemRights.Modify, AccessControlType.Allow)); if (UserUtil.IsSpecialUser(item)) //if (item == "Chow Chi To(周志滔,Sammy)" || item == "Ling Wai Man(凌慧敏,Velma)" || item == "Chan Fai Lung(陳輝龍,Onyx)" || item == "Ng Lau Yu, Lilith (吳柳如)" || // item == "Lee Miu Wah(李苗華)" || item == "Lee Ming Fung(李銘峯)" || item == "Ho Kin Hang(何健恒,Ken)" || item == "Yeung Wai, Gabriel (楊偉)") { string asText = string.Format("select as_userid from TB_USER_AS where as_user = N'{0}'", item.Trim()); string asId = DataService.GetInstance().ExecuteScalar(asText).ToString().Trim(); fs.AddAccessRule(new FileSystemAccessRule(asId, FileSystemRights.Modify, AccessControlType.Allow)); } string sharedVpath = sharedDivision != GlobalService.Division && folder.StartsWith(@"\" + GlobalService.Division) ? @"\Documents" + folder : sharedDepartment != GlobalService.Department && folder.StartsWith(@"\Common") ? @"\Documents" + folder : folder; if (sharedVpath.Contains("'")) { sharedVpath.Replace("'", "''"); } string sharedText = string.Format("insert into " + tableName + " (r_filename, r_extension, r_keyword, r_lastaccess, r_lastmodified, r_owner, r_shared, r_path, r_vpath, r_deletedate)" + " values (N'{0}', '{1}', N'{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', '{9}')", name, extension, keyword, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), modified, GlobalService.User, item.Trim(), sPath, sharedVpath, "2099/12/31"); queryList.Add(sharedText); try { File.SetAccessControl(path, fs); } catch (Exception ex) { Debug.WriteLine(ex.Message + ex.StackTrace); continue; } } if (cnlist.Count > 0) { PermissionUtil.SetGlobalPermission(cnlist, path, "kmcn.local"); SharedUtil.SharedCN(cnlist, sPath, name, keyword); } if (vnlist.Count > 0) { PermissionUtil.SetGlobalPermission(vnlist, path, "kdtvn.local"); SharedUtil.SharedVN(vnlist, sPath, name, keyword); } if (jplist.Count > 0) { PermissionUtil.SetGlobalPermission(jplist, path, "km.local"); SharedUtil.SharedJp(jplist, sPath, name, keyword); } } try { List <string> receiverlist = cnlist.Concat(vnlist).Concat(jplist).ToList(); if (receiverlist.Count > 0) { EmailUtil.SendNotificationEmail(receiverlist); } } catch (Exception ex) { Debug.WriteLine(ex.Message + ex.StackTrace); } if (shared == "") { shared = "-"; } string ownerText = string.Format("update " + GlobalService.DbTable + " set r_shared = N'{0}' where r_path = N'{1}'", shared, sPath); queryList.Add(ownerText); } foreach (string text in queryList) { DataService.GetInstance().ExecuteNonQuery(text); } GlobalService.RootTable = RootUtil.RootDataTable(); this.DialogResult = DialogResult.OK; }
public static void DeleteData(DataTable table, string path) { string sPath = path.Contains("'") ? path.Replace("'", "''") : path; //DataRow[] rows = table.Select(string.Format("filepath = '{0}'", sPath)); DataRow[] rows = (from row in table.AsEnumerable() where row.RowState != DataRowState.Deleted && row.RowState != DataRowState.Deleted && row.Field <string>("filepath") == path select row).ToArray(); List <string> queryList = new List <string>(); foreach (DataRow row in rows) { string owner = row["fileowner"].ToString().Trim(); if (owner == GlobalService.User) { List <string> sharedList = GetSharedList(GlobalService.DbTable, sPath); foreach (string shared in sharedList) { if (shared == "-") { continue; } if (!UserUtil.IsCnMember(shared.Trim()) && !UserUtil.IsVnMember(shared.Trim()) && !UserUtil.IsJpMember(shared.Trim())) { string tableName = "TB_" + AdUtil.GetUserIdByUsername(shared, "kmhk.local"); string sharedText = string.Format("delete from " + tableName + " where r_path = N'{0}'", sPath); queryList.Add(sharedText); } else { string sharedText = string.Format("delete from TB_OUTSIDE_SHARE where o_path = N'{0}'", sPath); queryList.Add(sharedText); } } string ownerText = string.Format("delete from " + GlobalService.DbTable + " where r_path = N'{0}'", sPath); queryList.Add(ownerText); if (File.Exists(path)) { if (GlobalService.User == UserUtil.HrUserName1()) //if (GlobalService.User == "Ling Wai Man(凌慧敏,Velma)") { string directory = @"\\kdthk-dm1\project\IT System\MyCloud Record\" + DateTime.Today.ToString("yyyyMMdd"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filename = Path.GetFileName(path); File.Copy(path, directory + @"\" + filename); } File.Delete(path); } string delQuery = string.Format("delete from S_OUT_SHARE where o_path = N'{0}'", sPath); DataServiceMes.GetInstance().ExecuteNonQuery(delQuery); } else { string tableName = "TB_" + AdUtil.GetUserIdByUsername(owner, "kmhk.local"); List <string> sharedList = GetSharedList(tableName, sPath); sharedList.Remove(GlobalService.User); string shared = string.Join(";", sharedList.ToArray()); if (shared == "") { shared = "-"; } string ownerText = string.Format("update " + tableName + " set r_shared = N'{0}' where r_path = N'{1}'", shared, sPath); queryList.Add(ownerText); string sharedText = string.Format("delete from " + GlobalService.DbTable + " where r_path = N'{0}'", sPath); queryList.Add(sharedText); } if (row.RowState != DataRowState.Deleted) { row.Delete(); } } string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); string query = string.Format("insert into TB_LOG (log_datetime, log_category, log_path, log_by) values ('{0}', '{1}', N'{2}', N'{3}')", now, "Delete", sPath, GlobalService.User); queryList.Add(query); foreach (string text in queryList) { DataService.GetInstance().ExecuteNonQuery(text); } //QueryUtil.InsertDataToLocalDb(text); }