Exemple #1
0
        public string UpdateUser(User user, string auditEvent = null)
        {
            try
            {
                var existinguser = App.dbcontext.Users.FirstOrDefault(a => a.Username == user.Username.Trim());
                if (existinguser == null || existinguser.UserId == user.UserId)
                {
                    user.UpdatedBy   = App.LoggedInUser.UserId;
                    user.UpdatedDt   = DateTime.Now;
                    user.Password    = CryptoProvider.HashPassword(user.Password);
                    user.LockCounter = App.LoggedInUser.LockCounter;

                    Audit audit = new Audit
                    {
                        RecordId    = user.UserId,
                        Type        = AuditTypes.User.ToString(),
                        Description = auditEvent,
                        UpdatedBy   = App.LoggedInUser.UserId,
                        UpdatedDt   = DateTime.Now
                    };

                    App.dbcontext.Audits.Add(audit);
                    App.dbcontext.SaveChanges();
                }
                else
                {
                    return(Messages.UsernameTaken);
                }
            }
            catch (Exception ex)
            {
                var logNumber = Logger.Log(nameof(UpdateUser), new Dictionary <string, object>
                {
                    { LogConsts.Exception, ex }
                });

                ex.Data.Add(nameof(logNumber), logNumber);
                throw ex;
            }

            return(null);
        }
Exemple #2
0
        public string CreateUser(User user, string auditEvent = null)
        {
            try
            {
                var existinguser = App.dbcontext.Users.FirstOrDefault(a => a.Username == user.Username.Trim());
                if (existinguser == null)
                {
                    user.CreatedBy             = App.LoggedInUser.UserId;
                    user.CreatedDt             = DateTime.Now;
                    user.UserId                = Guid.NewGuid().ToString();
                    user.RequirePasswordChange = true;
                    user.Password              = string.IsNullOrEmpty(user.Password) ? CryptoProvider.HashPassword(UsersConsts.DefaultTempPassword) : CryptoProvider.HashPassword(user.Password);

                    App.dbcontext.Users.Add(user);

                    Audit audit = new Audit
                    {
                        RecordId    = user.UserId,
                        Type        = AuditTypes.User.ToString(),
                        Description = auditEvent,
                        UpdatedBy   = App.LoggedInUser.UserId,
                        UpdatedDt   = DateTime.Now
                    };

                    App.dbcontext.Audits.Add(audit);
                    App.dbcontext.SaveChanges();
                }
                else
                {
                    return(Messages.UsernameTaken);
                }
            }
            catch (Exception ex)
            {
                var logNumber = Logger.Log(nameof(CreateUser), new Dictionary <string, object>
                {
                    { LogConsts.Exception, ex }
                });

                ex.Data.Add(nameof(logNumber), logNumber);
                throw ex;
            }

            return(string.Empty);
        }