Esempio n. 1
0
        public async Task <Model.Identity> Invoke(IUnitOfWork uow)
        {
            var dbContext = DbContext.From(uow);

            var existing = await dbContext.Identity.Query(new IdentityFilter
            {
                Email = _identity.Email
            });

            if (existing?.FirstOrDefault(e => e.Email.ToLower().Equals(_identity.Email.ToLower())) != null)
            {
                throw new BadRequestException(string.Format(
                                                  await dbContext.Translation.GetTranslation(Locale.EN, "IdentityEmailExsits"),
                                                  _identity.Email));
            }

            uow.BeginTransaction();

            _identity.ID = Guid.NewGuid();

            // To fix permission length when stored to DB
            if ((_identity.Permissions?.Length ?? 0) < (int)Permission.SystemReadWrite)
            {
                PermissionUtil.SetPermission(_identity.Permissions,
                                             (int)Permission.SystemReadWrite,
                                             Access.None,
                                             out string fixedPermissions);

                _identity.Permissions = fixedPermissions;
            }

            await dbContext.Identity.Insert(_identity);

            await dbContext.Identity.MapIdentityToClusters(_identity.ID, _identity.Clusters);

            await dbContext.Identity.MapIdentityToRequestTypes(_identity.ID, _identity.RequestTypes);

            // Create credential

            var tempKey = RandomStringGenerator.Generate();

            await dbContext.Credential.InsertCredential(new Credential
            {
                Key         = _identity.Email.ToLower(),
                Secret      = Encryptor.Encrypt(Secret.Key, tempKey),
                Provider    = Provider.Default,
                IdentityID  = _identity.ID,
                IsTemporary = true
            });

            var template = await dbContext.EmailTemplate.GetTemplate(EmailTemplateType.NewAccount);

            _smtpProvider.Send(new Mail
            {
                From       = template.From,
                Recipients = new string[] { _identity.Email },
                Subject    = template.Title,
                Body       = template.Body.Replace("{email}", _identity.Email)
                             .Replace("{username}", _identity.Email.Replace("@muis.gov.sg", ""))
                             .Replace("{secret}", tempKey),
                IsBodyHtml = template.IsBodyHtml
            });

            var logID = await dbContext.Log.InsertLog(new Log
            {
                Action   = await dbContext.Translation.GetTranslation(Locale.EN, "IdentityCreated"),
                UserID   = _user.ID,
                UserName = _user.Name
            });

            await dbContext.Identity.MapIdentityToLog(_identity.ID, logID);

            var result = await dbContext.Identity.GetIdentityByID(_identity.ID);

            uow.Commit();

            return(result);
        }
Esempio n. 2
0
        public async Task <Model.Identity> Invoke(IUnitOfWork uow)
        {
            var dbContext = DbContext.From(uow);

            var existing = await dbContext.Identity.GetIdentityByID(_identity.ID);

            if (existing == null)
            {
                throw new NotFoundException();
            }

            var others = await dbContext.Identity.Query(new IdentityFilter
            {
                Email = _identity.Email
            });

            if (others.FirstOrDefault(e => e.ID != _identity.ID &&
                                      e.Email.ToLower().Equals(_identity.Email.ToLower())) != null)
            {
                throw new BadRequestException(string.Format(
                                                  await dbContext.Translation.GetTranslation(Locale.EN, "IdentityEmailExsits"),
                                                  _identity.Email));
            }

            uow.BeginTransaction();

            var hasChanges = await CreateLog(dbContext, existing, _identity);

            if (hasChanges)
            {
                // To fix permission length when stored to DB
                if ((_identity.Permissions?.Length ?? 0) < (int)Permission.SystemReadWrite)
                {
                    PermissionUtil.SetPermission(_identity.Permissions,
                                                 (int)Permission.SystemReadWrite,
                                                 Access.None,
                                                 out string fixedPermissions);

                    _identity.Permissions = fixedPermissions;
                }

                await dbContext.Identity.Update(_identity);

                await dbContext.Identity.MapIdentityToClusters(_identity.ID, _identity.Clusters);

                await dbContext.Identity.MapIdentityToRequestTypes(_identity.ID, _identity.RequestTypes);

                // Update credential
                if (!existing.Email.Equals(_identity.Email, StringComparison.InvariantCultureIgnoreCase))
                {
                    var credential = await dbContext.Credential.GetCredentialByKey(
                        Model.Provider.Default,
                        existing.Email);

                    credential.Key = _identity.Email.ToLower();

                    await dbContext.Credential.UpdateCredential(credential);
                }

                // Remove from cache
                await _cacheProvider.RemoveAsync(_identity.ID.ToString());

                _eventBus.Publish(new OnIdentityChangedEvent
                {
                    ID   = _identity.ID,
                    Name = _identity.Name
                });
            }

            var result = await dbContext.Identity.GetIdentityByID(_identity.ID);

            uow.Commit();

            return(result);
        }
Esempio n. 3
0
        private void SaveData()
        {
            SharedUtil.UpdateEmptyShared();
            SharedUtil.UpdateShared();

            string sPath = _filePath.Contains("'") ? _filePath.Replace("'", "''") : _filePath;

            DataRow[] rows = GlobalService.RootTable.Select(string.Format("filepath = '{0}'", sPath));

            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 (DataGridViewRow row in dgvUser.Rows)
            {
                string loc = row.Cells[1].Value.ToString().Trim();
                if (loc == "HK")
                {
                    hklist.Add(row.Cells[0].Value.ToString().Trim());
                }
                else if (loc == "CN")
                {
                    cnlist.Add(row.Cells[0].Value.ToString().Trim());
                }
                else if (loc == "VN")
                {
                    vnlist.Add(row.Cells[0].Value.ToString().Trim());
                }
                else
                {
                    jplist.Add(row.Cells[0].Value.ToString().Trim());
                }
            }

            //FileInfo info = new FileInfo(_filePath);
            //FileSecurity fs = info.GetAccessControl();
            //AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(NTAccount));

            if (deleteList.Count > 0)
            {
                PermissionUtil.RemovePermission(deleteList, _filePath);
            }

            if (deleteCnList.Count > 0)
            {
                PermissionUtil.RemoveGlobalPermission(deleteCnList, _filePath, "kmcn.local");
            }

            if (deleteVnList.Count > 0)
            {
                PermissionUtil.RemoveGlobalPermission(deleteVnList, _filePath, "kdtvn.local");
            }

            if (deleteJpList.Count > 0)
            {
                PermissionUtil.RemoveGlobalPermission(deleteJpList, _filePath, "km.local");
            }

            if (hklist.Count > 0)
            {
                PermissionUtil.SetPermission(hklist, _filePath);
            }

            if (cnlist.Count > 0)
            {
                PermissionUtil.SetGlobalPermission(cnlist, _filePath, "kmcn.local");
            }

            if (vnlist.Count > 0)
            {
                PermissionUtil.SetGlobalPermission(vnlist, _filePath, "kdtvn.local");
            }

            if (jplist.Count > 0)
            {
                PermissionUtil.SetGlobalPermission(jplist, _filePath, "km.local");
            }

            List <string> totalList = hklist.Concat(cnlist).Concat(vnlist).Concat(jplist).ToList();

            string shared = totalList.Count == 0 ? "-" : string.Join(";", totalList.ToArray());

            foreach (DataRow row in rows)
            {
                if (shared == "")
                {
                    shared = "-";
                }

                row["shared"] = shared;

                string text = string.Format("update " + GlobalService.DbTable + " set r_shared = N'{0}' where r_path = N'{1}'", shared, sPath);
                DataService.GetInstance().ExecuteNonQuery(text);
            }

            string disc = DiscUtil.IsDisc(GlobalService.RootTable, _filePath) ? "True" : "False";

            string extension = Path.GetExtension(_filePath);

            if (_fileName.Contains("'"))
            {
                _fileName = _fileName.Replace("'", "''");
            }

            if (_keyword.Contains("'"))
            {
                _keyword = _keyword.Replace("'", "''");
            }

            foreach (string sharedPerson in hklist)
            {
                string tableName = "TB_" + AdUtil.GetUserIdByUsername(sharedPerson.Trim(), "kmhk.local");

                string sharedDivision   = SystemUtil.GetDivision(sharedPerson.Trim());
                string sharedDepartment = SystemUtil.GetDepartment(sharedPerson.Trim());

                string sharedVpath = sharedDivision != GlobalService.Division && _vpath.StartsWith(@"\" + GlobalService.Division) ? @"\Documents" + _vpath
                            : sharedDepartment != GlobalService.DepartmentFolder && _vpath.StartsWith(@"\Common") ? @"\Documents" + _vpath : _vpath;

                string sharedText = string.Format("if not exists (select * from " + tableName + " where r_path = N'{7}') insert into " + tableName + " (r_filename, r_extension, r_keyword, r_lastaccess, r_lastmodified, r_owner, r_shared, r_path, r_vpath, r_deletedate, r_disc)" +
                                                  " values (N'{0}', '{1}', N'{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', '{9}', '{10}')", _fileName, extension, _keyword, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), _lastModified, GlobalService.User,
                                                  sharedPerson.Trim(), sPath, sharedVpath, "2099/12/31", disc);

                DataService.GetInstance().ExecuteNonQuery(sharedText);
            }

            SharedUtil.SharedCN(cnlist, sPath, _fileName, _keyword);
            SharedUtil.SharedVN(vnlist, sPath, _fileName, _keyword);
            SharedUtil.SharedJp(jplist, sPath, _fileName, _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);
            }

            DialogResult = DialogResult.OK;
        }