Example #1
0
        public static User RetrieveUserByUsername(string username)
        {
            try
            {
                var existingUser = new User();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingUser = context.Users
                                    .Where(t => t.Username.Equals(username))
                                    .FirstOrDefault();
                }

                return existingUser;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
        public static bool ChangePassword(string username, string newHashedPassword)
        {
            try
            {
                User existingUser = new User();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingUser = context.Users
                                    .Where(t => t.Username == username)
                                    .FirstOrDefault();
                }

                if (existingUser != null)
                {
                    existingUser.HashedPassword = newHashedPassword;
                    existingUser.FirstTime = false;
                    using (var context = new PrinterMonitorDBEntities())
                    {
                        context.Entry(existingUser).State = EntityState.Modified;

                        context.SaveChanges();
                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #3
0
        public static bool UserExists(string username, string password)
        {
            try
            {
                var user = new User();

                user = UserDL.AuthenticateUser(username, PasswordHash.MD5Hash(password));

                if (user == null)
                    return false;
                else
                    return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #4
0
 public static bool Update(User user)
 {
     try
     {
         return UserDL.Update(user);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #5
0
 public static bool Save(User user, out string message)
 {
     try
     {
         if (UserDL.UserExists(user))
         {
             message = string.Format("User with username: {0} exists already", user.Username);
             return false;
         }
         else
         {
             message = string.Empty;
             if (UserDL.Save(user))
             {
                 return true;
             }
             else
                 return false;
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #6
0
        public static void SendForgotPasswordMail(User user)
        {
            try
            {
                string key = System.Configuration.ConfigurationManager.AppSettings.Get("ekey");
                string encrypted_username = Crypter.Encrypt(key, user.Username);

                string userFullName = user.Lastname + " " + user.Othernames;

                string organization = System.Configuration.ConfigurationManager.AppSettings.Get("Organization");
                string applicationName = System.Configuration.ConfigurationManager.AppSettings.Get("ApplicationName");
                string websiteUrl = System.Configuration.ConfigurationManager.AppSettings.Get("WebsiteUrl");
                string passwordResetUrl = websiteUrl + "User/ResetPassword?rq=" + encrypted_username; ;
                string subject = "Password Reset Request on " + applicationName;

                string fromAddress = "";
                string smtpUsername = "";
                string smtpPassword = "";
                string smtpHost = "";
                Int32 smtpPort = 587;
                bool smtpUseDefaultCredentials = false;
                bool smtpEnableSsl = true;

                MailHelper mailConfig = ConfigurationManager.GetSection("mailHelperSection") as MailHelper;
                if (mailConfig != null && mailConfig.Mail != null)
                {
                    fromAddress = mailConfig.Mail.FromEmailAddress;
                    smtpUsername = mailConfig.Mail.Username;
                    smtpPassword = mailConfig.Mail.Password;
                }

                if (mailConfig != null && mailConfig.Smtp != null)
                {
                    smtpHost = mailConfig.Smtp.Host;
                    smtpPort = Convert.ToInt32(mailConfig.Smtp.Port);
                    smtpUseDefaultCredentials = Convert.ToBoolean(mailConfig.Smtp.UseDefaultCredentials);
                    smtpEnableSsl = Convert.ToBoolean(mailConfig.Smtp.EnableSsl);
                }

                string body = "";

                body = System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/MailTemplates/ForgotPassword.txt"));
                body = body.Replace("#Organization", organization);
                body = body.Replace("#ApplicationName", applicationName);
                body = body.Replace("#UserFullName", userFullName);
                body = body.Replace("#WebsiteUrl", websiteUrl);
                body = body.Replace("#PasswordResetUrl", passwordResetUrl);

                Thread email = new Thread(delegate()
                {
                    Mail.SendMail(user.Email, fromAddress, subject, body, smtpHost, smtpPort, smtpUseDefaultCredentials, smtpUsername, smtpPassword, smtpEnableSsl);

                });

                email.IsBackground = true;
                email.Start();

            }
            catch (Exception ex)
            {
                ErrorHandler.WriteError(ex);
                throw ex;
            }
        }
Example #7
0
        public static void SendNewUserMail(User user)
        {
            try
            {
                Role role = RolePL.RetrieveRoleByID(user.UserRole);

                string userFullName = user.Lastname + " " + user.Othernames;
                string userUsername = user.Username;
                string userPassword = user.HashedPassword;
                string userRole = role.Name;
                string organization = System.Configuration.ConfigurationManager.AppSettings.Get("Organization");
                string applicationName = System.Configuration.ConfigurationManager.AppSettings.Get("ApplicationName");
                string websiteUrl = System.Configuration.ConfigurationManager.AppSettings.Get("WebsiteUrl");
                string subject = "Welcome to " + applicationName;
                string userFunction = "";

                foreach(RoleFunction roleFunction in role.RoleFunctions)
                {
                    userFunction += roleFunction.Function.Name + "<br/>";
                }

                string fromAddress = "";
                string smtpUsername = "";
                string smtpPassword = "";
                string smtpHost = "";
                Int32 smtpPort = 587;
                bool smtpUseDefaultCredentials = false;
                bool smtpEnableSsl = true;

                MailHelper mailConfig = ConfigurationManager.GetSection("mailHelperSection") as MailHelper;
                if (mailConfig != null && mailConfig.Mail != null)
                {
                    fromAddress = mailConfig.Mail.FromEmailAddress;
                    smtpUsername = mailConfig.Mail.Username;
                    smtpPassword = mailConfig.Mail.Password;
                }

                if (mailConfig != null && mailConfig.Smtp != null)
                {
                    smtpHost = mailConfig.Smtp.Host;
                    smtpPort = Convert.ToInt32(mailConfig.Smtp.Port);
                    smtpUseDefaultCredentials = Convert.ToBoolean(mailConfig.Smtp.UseDefaultCredentials);
                    smtpEnableSsl = Convert.ToBoolean(mailConfig.Smtp.EnableSsl);
                }

                string body = "";

                body = System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/MailTemplates/NewUser.txt"));
                body = body.Replace("#Organization", organization);
                body = body.Replace("#ApplicationName", applicationName);
                body = body.Replace("#UserFullName", userFullName);
                body = body.Replace("#Username", userUsername);
                body = body.Replace("#Password", userPassword);
                body = body.Replace("#Role", userRole);
                body = body.Replace("#UserFunctions", userFunction);
                body = body.Replace("#WebsiteUrl", websiteUrl);

                Thread email = new Thread(delegate()
                {
                    Mail.SendMail(user.Email, fromAddress, subject, body, smtpHost, smtpPort, smtpUseDefaultCredentials, smtpUsername, smtpPassword, smtpEnableSsl);

                });

                email.IsBackground = true;
                email.Start();

            }
            catch (Exception ex)
            {
                ErrorHandler.WriteError(ex);
                throw ex;
            }
        }
Example #8
0
        public static bool UpdateSmartCardID(long smartCardID, long userID, bool status)
        {
            try
            {
                var sc = new SmartCard();
                var user = new User();
                using (var context = new PrinterMonitorDBEntities())
                {
                    sc = context.SmartCards
                                    .Where(t => t.ID == smartCardID)
                                    .FirstOrDefault();

                    user = context.Users
                                    .Include(u => u.SmartCard)
                                    .Where(t => t.ID == userID)
                                    .FirstOrDefault();
                }

                if (sc != null && user != null)
                {
                    sc.Allocated = status;

                    if (status)
                    {
                        if (user.SmartCard != null)
                            throw new Exception(string.Format("User {0} has a smart card allocated to it already", user.Username));

                        user.SmartCardID = smartCardID;
                    }
                    else
                    {
                        user.SmartCard = null;
                        sc.Users = null;
                        user.SmartCardID = null;
                    }

                    using (var context = new PrinterMonitorDBEntities())
                    {
                        //Transaction block
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            try
                            {
                                context.Entry(user).State = EntityState.Modified;
                                context.SaveChanges();

                                context.Entry(sc).State = EntityState.Modified;
                                context.SaveChanges();

                                transaction.Commit();
                            }
                            catch (Exception ex)
                            {
                                transaction.Rollback();
                                throw ex;
                            }
                        }

                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #9
0
        public static bool UserExists(User user)
        {
            try
            {
                var existingUser = new User();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingUser = context.Users
                                    .Where(t => t.Username.Equals(user.Username))
                                    .FirstOrDefault();
                }

                if (existingUser == null)
                    return false;
                else
                    return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #10
0
        public static bool Update(User user)
        {
            try
            {
                User existingUser = new User();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingUser = context.Users
                                    .Where(t => t.ID == user.ID)
                                    .FirstOrDefault();
                }

                if (existingUser != null)
                {
                    existingUser.Email = user.Email;
                    existingUser.Gender = user.Gender;
                    existingUser.PhoneNumber = user.PhoneNumber;
                    existingUser.Lastname = user.Lastname;
                    existingUser.Othernames = user.Othernames;
                    existingUser.UserRole = user.UserRole;
                    existingUser.UserBranch = user.UserBranch;

                    using (var context = new PrinterMonitorDBEntities())
                    {
                        context.Entry(existingUser).State = EntityState.Modified;

                        context.SaveChanges();
                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #11
0
 public static bool Save(User user)
 {
     try
     {
         using (var context = new PrinterMonitorDBEntities())
         {
             context.Users.Add(user);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }