Esempio n. 1
0
 public static bool Save(Role role)
 {
     try
     {
         using (var context = new PrinterMonitorDBEntities())
         {
             context.Roles.Add(role);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 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;
            }
        }
Esempio n. 3
0
        public static bool Update(Function function)
        {
            try
            {
                Function existingfunction = new Function();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingfunction = context.Functions
                                    .Where(t => t.ID == function.ID)
                                    .FirstOrDefault();
                }

                if (existingfunction != null)
                {
                    existingfunction.Name = function.Name;
                    existingfunction.PageLink = function.PageLink;

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

                        context.SaveChanges();
                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 4
0
 public static bool Save(Function function)
 {
     try
     {
         using (var context = new PrinterMonitorDBEntities())
         {
             context.Functions.Add(function);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 5
0
 public static bool Save(Branch branch)
 {
     try
     {
         using (var context = new PrinterMonitorDBEntities())
         {
             context.Branches.Add(branch);
             context.SaveChanges();
         }
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 6
0
        public static bool Update(Branch branch)
        {
            try
            {
                Branch existingBranch = new Branch();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingBranch = context.Branches
                                    .Include(b => b.Printers)
                                    .Include(b => b.Users)
                                    .Where(t => t.ID == branch.ID)
                                    .FirstOrDefault();
                }

                if (existingBranch != null)
                {
                    existingBranch.Name = branch.Name;
                    existingBranch.Code = branch.Code;
                    existingBranch.Address = branch.Address;

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

                        context.SaveChanges();
                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 7
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;
            }
        }
Esempio n. 8
0
        public static bool InsertToken(string token, string username, string smartCardID)
        {
            try
            {
                var webToken = new CHECK_WEBTOKEN();
                webToken.DateOfrequest = System.DateTime.Now;
                webToken.Token = token;
                webToken.HashedToken = PasswordHash.MD5Hash(token);
                webToken.Username = username;
                webToken.SmartCardID = Crypter.Encrypt(System.Configuration.ConfigurationManager.AppSettings.Get("ekey"), smartCardID);
                webToken.HashedSmartCardID = PasswordHash.MD5Hash(smartCardID);
                webToken.Status = 0;

                using (var context = new PrinterMonitorDBEntities())
                {
                    // Saves the token
                    context.CHECK_WEBTOKEN.Add(webToken);
                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 9
0
        public static bool InsertSmartCardID(string smartCardID, out string response)
        {
            try
            {
                var sc = new SmartCard();
                string hashedSmartID = PasswordHash.MD5Hash(smartCardID);
                using (var context = new PrinterMonitorDBEntities())
                {
                    // Query for the token
                    sc = context.SmartCards
                                    .Where(t => t.HashedSmartCardID == hashedSmartID)
                                    .FirstOrDefault();
                }

                if (sc != null)
                {
                    response = "Smard Card ID has been registered already";
                    return false;
                }
                else
                {
                    var smartCard = new SmartCard();
                    smartCard.Allocated = false;
                    smartCard.EncryptedSmartCardID = Crypter.Encrypt(System.Configuration.ConfigurationManager.AppSettings.Get("ekey"), smartCardID);
                    smartCard.HashedSmartCardID = PasswordHash.MD5Hash(smartCardID);

                    using (var context = new PrinterMonitorDBEntities())
                    {
                        context.SmartCards.Add(smartCard);
                        context.SaveChanges();
                    }

                    response = "Successful";
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 10
0
        public static bool InsertEncrptedToken(CHECK_WEBTOKEN webToken)
        {
            try
            {
                CHECK_WEBTOKEN token;
                using (var context = new PrinterMonitorDBEntities())
                {
                    // Query for the token
                    token = context.CHECK_WEBTOKEN
                                    .Where(t => t.id == webToken.id)
                                    .FirstOrDefault();
                }

                //Insert the Encrypted Token against the Token supplied and Update Status to 1
                if (token != null)
                {
                    token.EncyptedToken = Crypter.Encrypt(System.Configuration.ConfigurationManager.AppSettings.Get("ekey"), webToken.Token);

                    token.Status = 1;
                }

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

                    context.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 11
0
        public static bool Update(Printer printer)
        {
            try
            {
                Printer existingPrinter = new Printer();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingPrinter = context.Printers
                                    .Where(t => t.ID == printer.ID)
                                    .FirstOrDefault();
                }

                if (existingPrinter != null)
                {
                    existingPrinter.PrinterSrNo = printer.PrinterSrNo;
                    existingPrinter.PrinterName = printer.PrinterName;
                    existingPrinter.PrinterBrand = printer.PrinterBrand;
                    existingPrinter.BranchID = printer.BranchID;

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

                        context.SaveChanges();
                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 12
0
        public static bool Update(Role role)
        {
            try
            {
                Role existingRole = new Role();
                using (var context = new PrinterMonitorDBEntities())
                {
                    existingRole = context.Roles
                                    .Where(t => t.ID == role.ID)
                                    .FirstOrDefault();
                }

                if (existingRole != null)
                {
                    existingRole.Name = role.Name;

                    using (var context = new PrinterMonitorDBEntities())
                    {
                        //Transaction block
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            try
                            {
                                //Modifying just the property details
                                context.Entry(existingRole).State = EntityState.Modified;
                                context.SaveChanges();

                                //Delete existing role function of the role
                                IEnumerable<RoleFunction> existingRoleFunctions = context.RoleFunctions.Include("Role")
                                                                .Where(t => existingRole.ID.Equals(t.RoleID))
                                                                .ToList();

                                if (existingRoleFunctions != null && existingRoleFunctions.ToList().Count != 0)
                                {
                                    context.RoleFunctions.RemoveRange(existingRoleFunctions);
                                    context.SaveChanges();
                                }

                                //Adding new Role Functions
                                List<RoleFunction> newRoleFunctions = new List<RoleFunction>();
                                foreach (RoleFunction function in role.RoleFunctions)
                                {
                                    RoleFunction roleFunction = new RoleFunction();
                                    roleFunction.RoleID = existingRole.ID;
                                    roleFunction.FunctionID = function.FunctionID;

                                    newRoleFunctions.Add(roleFunction);
                                }
                                if (newRoleFunctions != null && newRoleFunctions.Count != 0)
                                {
                                    context.RoleFunctions.AddRange(newRoleFunctions);
                                    context.SaveChanges();
                                }

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

                    }

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 13
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;
            }
        }