Exemple #1
0
        public Contracts.User.User Update(Contracts.User.UpdateUser update)
        {
            using (var db = new RAAPMasterEntities())
            {
                var user = db.Users.FirstOrDefault(u => u.UserId == update.UserId);
                if (user == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                if (db.Users.Any(a => a.Email == update.Email && a.UserId != update.UserId))
                {
                    throw new RAAPConflictException("Email is already in use!");
                }

                if (db.Users.Any(a => a.Username == update.Username && a.UserId != update.UserId))
                {
                    throw new RAAPConflictException("Username is already in use!");
                }

                user.ApplyUpdate(update);
                user.Roles.Clear();
                db.SaveChanges();

                foreach (var role in update.Roles)
                {
                    var roleToAdd = db.Roles.FirstOrDefault(r => r.Role1 == role);
                    roleToAdd.Users.Add(user);
                }

                db.SaveChanges();

                return(user.ToContract(true));
            }
        }
Exemple #2
0
        public void RecoverPassword(string username)
        {
            using (var db = new RAAPMasterEntities())
            {
                var dataUser = db.Users.FirstOrDefault(u => u.Username == username);
                if (dataUser == null)
                {
                    throw new RAAPConflictException("Invalid username!");
                }

                dataUser.NewPasswordGuid = Guid.NewGuid();
                db.SaveChanges();

                var mailMessage = new MailMessage
                {
                    Subject    = "CRMAP - Recover your password",
                    IsBodyHtml = false,
                    Body       = "Hi," + Environment.NewLine + Environment.NewLine +
                                 "To reset your CRMAP password, please use the following link:" + Environment.NewLine +
                                 "https://www.kamude.no/WebClient/changepassword/" + dataUser.NewPasswordGuid + Environment.NewLine +
                                 "" + Environment.NewLine +
                                 "Best regards," + Environment.NewLine +
                                 "CRMAP Team" + Environment.NewLine +
                                 "https://www.kamude.no/"
                };

                mailMessage.To.Add(new MailAddress(dataUser.Email));
                var mailClient = new SmtpClient();
                mailClient.Send(mailMessage);
            }
        }
Exemple #3
0
        public void Update(List <Contracts.Soa.SoaChapter> chapters)
        {
            var isoCodes = chapters.Select(c => c.IsoCode).Distinct();

            if (isoCodes.Count() > 1)
            {
                throw new ArgumentException("Multiple IsoCodes", "IsoCode");
            }
            var isoCode = isoCodes.First();

            var types = chapters.Select(c => c.SoaType).Distinct();

            if (types.Count() > 1)
            {
                throw new ArgumentException("Multiple SoA types", "SoaType");
            }
            var type = types.First();

            using (var db = new RAAPMasterEntities())
            {
                //Update existing chapters
                //Delete chapters not in db
                var dbChapters = db.SoaChapters.Where(c => c.SoaType == type).ToDictionary(c => c.Id);
                UpdateSoaChapters(chapters, dbChapters, isoCode);
                db.SoaChapters.RemoveRange(dbChapters.Values);

                //Add new chapters to db
                AddNewSoaChapters(chapters, null, db, isoCode);

                db.SaveChanges();
            }
        }
Exemple #4
0
        internal void SetNewPassword(Guid newPasswordGuid, string newPassword, string newPasswordRepeat)
        {
            using (var db = new RAAPMasterEntities())
            {
                var user = db.Users.FirstOrDefault(u => u.NewPasswordGuid == newPasswordGuid);
                if (user == null)
                {
                    throw new RAAPConflictException("Invalid password recovery id!");
                }

                if (string.IsNullOrEmpty(newPassword) || newPassword.Length < 8)
                {
                    throw new RAAPConflictException("Password must at least be 8 characters.");
                }

                if (newPassword != newPasswordRepeat)
                {
                    throw new RAAPConflictException("Passwords don't match! Please repeat your password.");
                }

                user.NewPasswordGuid = null;
                user.PasswordHash    = Common.Security.Password.ComputeHash(newPassword, user.CreatedOn.Date.Ticks.ToString());
                db.SaveChanges();
            }
        }
Exemple #5
0
        public Contracts.Soa.SoaFile AddFile(int soaChapterId, string fileName, bool masterDb = false)
        {
            fileName = Path.GetFileName(fileName);
            var dbFile = new SoaFile()
            {
                SoaChapterId = soaChapterId,
                FileName     = fileName,
                Guid         = Guid.NewGuid()
            };

            if (masterDb)
            {
                using (var db = new RAAPMasterEntities())
                {
                    db.SoaFiles.Add(dbFile);
                    db.SaveChanges();
                    return(dbFile.ToContract());
                }
            }
            else
            {
                using (var db = new RAAPEntities(GetConnectionString()))
                {
                    db.SoaFiles.Add(dbFile);
                    db.SaveChanges();
                    return(dbFile.ToContract());
                }
            }
        }
Exemple #6
0
        public void ChangePassword(Contracts.User.ChangePassword changePassword)
        {
            using (var db = new RAAPMasterEntities())
            {
                var dataUser = db.Users.FirstOrDefault(u => u.UserId == changePassword.UserId);
                if (dataUser == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                if (!Common.Security.Password.VerifyHash(dataUser.CreatedOn.Date.Ticks.ToString(), dataUser.PasswordHash, changePassword.Password))
                {
                    throw new RAAPConflictException("Invalid password!");
                }

                if (changePassword.NewPassword != changePassword.NewPasswordRepeat)
                {
                    throw new RAAPConflictException("Passwords it not matching!");
                }

                if (string.IsNullOrEmpty(changePassword.Password) || changePassword.Password.Length < 8)
                {
                    throw new RAAPConflictException("Password must at least be 8 characters.");
                }

                dataUser.PasswordHash = Common.Security.Password.ComputeHash(changePassword.NewPassword, dataUser.CreatedOn.Date.Ticks.ToString());
                db.SaveChanges();
            }
        }
Exemple #7
0
        public Contracts.User.User Create(CreateUser create)
        {
            try
            {
                using (var db = new RAAPMasterEntities())
                {
                    if (db.Users.Any(a => a.Email == create.Email))
                    {
                        throw new RAAPConflictException("Email is already in use!");
                    }

                    if (db.Users.Any(a => a.Username == create.Username))
                    {
                        throw new RAAPConflictException("Username is already in use!");
                    }

                    if (string.IsNullOrEmpty(create.Password) || create.Password.Length < 4)
                    {
                        throw new RAAPConflictException("Password must at least be 4 characters.");
                    }

                    if (create.Password != create.PasswordRepeat)
                    {
                        throw new RAAPConflictException("Please repeat your password correctly.");
                    }


                    var user = create.ToDataModel(db);
                    user.PasswordHash = Common.Security.Password.ComputeHash(create.Password,
                                                                             user.CreatedOn.Date.Ticks.ToString());

                    db.Users.Add(user);
                    db.SaveChanges();

                    // add default role to all users
                    //var sysadminRole = db.Roles.FirstOrDefault(r => r.Role1 == "SystemAdministrator");
                    //sysadminRole.Users.Add(user);
                    db.SaveChanges();

                    return(db.Users.Include("Company").FirstOrDefault(u => u.UserId == user.UserId).ToContract(true));
                }
            }
            catch (Exception exception)
            {
                throw;
            }
        }
Exemple #8
0
 public void Delete(int id)
 {
     using (var db = new RAAPMasterEntities())
     {
         var user = db.Users.FirstOrDefault(a => a.UserId == id);
         if (user == null)
         {
             throw new RAAPNotFoundException("Item not found.");
         }
         user.Roles.Clear();
         db.Users.Remove(user);
         db.SaveChanges();
     }
 }
Exemple #9
0
        public Contracts.Help.HelpEntry Create(Contracts.Help.CreateHelpEntry create)
        {
            using (var db = new RAAPMasterEntities())
            {
                if (db.Helps.Any(a => a.Slug == create.Slug && a.Language == create.Language))
                {
                    throw new RAAPConflictException("Slug & language combination is already in use!");
                }

                var help = create.ToDataModel();
                db.Helps.Add(help);
                db.SaveChanges();

                return(db.Helps.FirstOrDefault(u => u.HelpId == help.HelpId).ToContract());
            }
        }
Exemple #10
0
        public void UpdateProfileImage(int userId, byte[] image, string contentType)
        {
            using (var db = new RAAPMasterEntities())
            {
                var dbUser = db.Users.FirstOrDefault(u => u.UserId == userId);
                if (dbUser == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                dbUser.ProfileImage         = image;
                dbUser.ProfileImageFiletype = contentType;

                db.SaveChanges();
            }
        }
Exemple #11
0
        public void Delete(int id)
        {
            using (var db = new RAAPMasterEntities())
            {
                var company = db.Companies.FirstOrDefault(a => a.CompanyId == id);
                if (company == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                if (company.Users.Any())
                {
                    throw new RAAPConflictException("You must remove connected users before you delete this company");
                }

                db.Companies.Remove(company);
                db.SaveChanges();
            }
        }
Exemple #12
0
        internal Contracts.Soa.SoaChapter AddTemplate(Contracts.Soa.SoaChapter soa)
        {
            var isoCode = soa.IsoCode;

            if (soa.SoaType == 3)
            {
                using (var db = new RAAPMasterEntities())
                {
                    var chapter = soa.ToDataModel(null, db, isoCode);
                    db.SoaChapters.Add(chapter);
                    db.SaveChanges();
                    return(chapter.ToContract(isoCode));
                }
            }
            else
            {
                throw new ArgumentException("Invalid SoaType", "SoaType");
            }
        }
Exemple #13
0
        public void SetPassword(SetPassword setPassword)
        {
            using (var db = new RAAPMasterEntities())
            {
                var dataUser = db.Users.FirstOrDefault(u => u.UserId == setPassword.UserId);
                if (dataUser == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                if (string.IsNullOrEmpty(setPassword.Password) || setPassword.Password.Length < 4)
                {
                    throw new RAAPConflictException("Password must at least be 4 characters.");
                }

                dataUser.PasswordHash = Common.Security.Password.ComputeHash(setPassword.Password, dataUser.CreatedOn.Date.Ticks.ToString());
                db.SaveChanges();
            }
        }
Exemple #14
0
        public Contracts.Help.HelpEntry Update(Contracts.Help.UpdateHelpEntry update)
        {
            using (var db = new RAAPMasterEntities())
            {
                var help = db.Helps.FirstOrDefault(u => u.HelpId == update.Id);
                if (help == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                if (db.Helps.Any(a => a.Slug == update.Slug && a.Language == update.Language && a.HelpId != update.Id))
                {
                    throw new RAAPConflictException("Slug & language combination is already in use!");
                }

                help.ApplyUpdate(update);
                db.SaveChanges();
                return(help.ToContract());
            }
        }
Exemple #15
0
 public void DeleteFile(Contracts.Soa.SoaFile file)
 {
     if (file.Template)
     {
         using (var db = new RAAPMasterEntities())
         {
             var dbFile = db.SoaFiles.FirstOrDefault(f => f.Guid == file.Guid);
             db.SoaFiles.Remove(dbFile);
             db.SaveChanges();
         }
     }
     else
     {
         using (var db = new RAAPEntities(GetConnectionString()))
         {
             var dbFile = db.SoaFiles.FirstOrDefault(f => f.Guid == file.Guid);
             db.SoaFiles.Remove(dbFile);
             db.SaveChanges();
         }
     }
 }
Exemple #16
0
        public Contracts.Company.Company Update(Contracts.Company.UpdateCompany update)
        {
            using (var db = new RAAPMasterEntities())
            {
                var company = db.Companies.FirstOrDefault(c => c.CompanyId == update.CompanyId);
                if (company == null)
                {
                    throw new RAAPNotFoundException("Item not found.");
                }

                if (db.Companies.Any(a => a.Name == update.Name && a.CompanyId != update.CompanyId))
                {
                    throw new RAAPConflictException("Name is already in use, please try another name.");
                }

                company.ApplyUpdate(update);
                db.SaveChanges();

                return(company.ToContract());
            }
        }
Exemple #17
0
        public Contracts.Company.Company Create(Contracts.Company.CreateCompany create)
        {
            using (var db = new RAAPMasterEntities())
            {
                if (db.Companies.Any(a => a.Name == create.Name))
                {
                    throw new RAAPConflictException("Name is already in use, please try another name.");
                }
                var company = create.ToDataModel();

                using (var sqlConnection = new SqlConnection(db.Database.Connection.ConnectionString))
                {
                    sqlConnection.Open();

                    var sqlCommand = new SqlCommand(string.Format(SqlCreateCompany, company.DatabaseName), sqlConnection);
                    sqlCommand.ExecuteNonQuery();
                }

                db.Companies.Add(company);
                db.SaveChanges();

                return(company.ToContract());
            }
        }