コード例 #1
0
        /// <summary>
        /// Retrieve the module on depends on the settings and rights of the current user
        /// </summary>
        /// <param name="user"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public IModule GetModule(IUser user, int moduleId)
        {
            try
            {
                if (Database == null || user == null)
                {
                    return(null);
                }

                // Look for modules allowed for the current user

                UserModuleRecord userModuleFound = null;

                foreach (UserModuleRecord userModuleCurrent in (from userModule in Database.UserModule
                                                                where userModule.UserId == user.Id && userModule.ModuleId == moduleId
                                                                select userModule).ToList())
                {
                    InformationRecord information = Database._Information.Find("UserModule", userModuleCurrent.Id);

                    if (information == null || !information.IsDeleted)
                    {
                        userModuleFound = userModuleCurrent;
                        break;
                    }
                }

                if (userModuleFound == null)
                {
                    Warn($"No module '{moduleId}' found for the user '{user.Id}' ...");
                    return(null);
                }

                // Look for the module

                ModuleRecord module = Database.Module.Find(userModuleFound.ModuleId);

                if (module == null)
                {
                    Error($"Database inconsistency due to the missing of the module '{userModuleFound.ModuleId}' ...");
                    return(null);
                }

                if (!module.Enable)
                {
                    Warn($"The module '{module.Name}' is disabled!");
                    return(null);
                }

                Info($"The module is '{module.Name}'");
                return(module);
            }
            catch (System.Exception ex)
            {
                Exception($"Unable to retrieve the module '{moduleId}'", ex);
                return(null);
            }
        }
コード例 #2
0
        public ActionResult Update(int id, string name, string login, string email, string address, string comment)
        {
            Debug($"Get ~/Administration/Customer/Update(id={id}, name={name}, login={login}, email={email}, address={address}, comment={comment})");

            // Only for administrator from the first customer (Syncytium)

            if (!(_userManager.GetById(int.Parse(HttpContext.User.Identity.Name)) is UserRecord user) || user.CustomerId != 1)
            {
                return(HttpNotFound());
            }

            // The customer has to exist

            CustomerRecord customer = _userManager.Database.Customer.Find(id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            // check the value by itself

            Errors errors = new Errors();

            // no name set

            if (string.IsNullOrWhiteSpace(name))
            {
                errors.AddField("Name", "ERR_FIELD_REQUIRED", new object[] { "{CUSTOMER_NAME}" });
            }
            name = name.Trim();

            // no login set

            if (string.IsNullOrWhiteSpace(login))
            {
                errors.AddField("Login", "ERR_FIELD_REQUIRED", new object[] { "{CUSTOMER_LOGIN}" });
            }
            login = login.Trim();

            // no email set

            if (string.IsNullOrWhiteSpace(email))
            {
                errors.AddField("Email", "ERR_FIELD_REQUIRED", new object[] { "{CUSTOMER_EMAIL}" });
            }
            email = email.Trim();

            // check if the name already exists

            if (_userManager.Database.Customer.Where(c => c.Name.Equals(name) && c.Id != customer.Id).Any())
            {
                errors.AddField("Name", "ERR_FIELD_UNIQUE", new object[] { "{CUSTOMER_NAME}" });
            }

            if (!customer.Login.Equals(login))
            {
                // check if the new login already exists

                bool loginExist = false;
                foreach (UserRecord record in _userManager.Database.User.Where(u => u.Login.Equals(login) && u.CustomerId != customer.Id).ToList())
                {
                    // User deleted ?

                    InformationRecord information = _userManager.Database._Information.FirstOrDefault(info => info.Id == record.Id && info.Table.Equals("User"));
                    if (information == null || information.DeleteTick == null)
                    {
                        loginExist = true;
                        break;
                    }
                }

                if (loginExist)
                {
                    errors.AddField("Name", "ERR_FIELD_UNIQUE", new object[] { "{CUSTOMER_LOGIN}" });
                }
            }

            // load ressources before designing the screen fitted to the user's profile

            LanguageDictionary ressources = new LanguageDictionary(Server.MapPath(LanguageDictionary.DIRECTORY_IMAGE), ConfigurationManager.DefaultLanguage);

            ressources.Load(_userManager.Database, user.CustomerId);

            if (errors.HasError)
            {
                // update ModelState on depends on errors

                SetModelState(ModelState, ressources, errors);
                return(View(new CustomerViewModel(ressources, user, new CustomerRecord {
                    Name = name, Login = login, Email = email, Address = address, Comment = comment
                })));
            }

            // Update the customer

            Info($"Updating the customer ({customer}) within ('{name}', '{login}', '{email}', '{address}', '{comment}') ...");

            // look for the administrator exists

            UserRecord administrator = null;

            foreach (UserRecord record in _userManager.Database.User.Where(u => u.Login.Equals(customer.Login) && u.CustomerId == customer.Id).ToList())
            {
                // User deleted ?

                InformationRecord information = _userManager.Database._Information.FirstOrDefault(info => info.Id == record.Id && info.Table.Equals("User"));
                if (information == null || information.DeleteTick == null)
                {
                    administrator = record;
                    break;
                }
            }

            bool sendEmail = false;

            if (administrator == null)
            {
                Info($"The administrator '{customer.Login}' was removed!");

                administrator = new UserRecord()
                {
                    Login        = login,
                    Registration = name,
                    Name         = name,
                    Email        = email,
                    Language     = user.Language,
                    CustomerId   = customer.Id
                };
                Info($"Creating a new administrator {administrator} ...");

                _userManager.Database.User.Add(administrator);
                _userManager.Database.SaveChanges();

                sendEmail = true;
            }
            else if (!administrator.Login.Equals(login) || !administrator.Email.Equals(email))
            {
                Info($"The administrator '{administrator}' has to be updated!");

                if (!administrator.Login.Equals(login))
                {
                    administrator.Login = login;
                }

                if (administrator.Registration.Equals(customer.Name))
                {
                    administrator.Registration = name;
                }

                if (administrator.Name.Equals(customer.Name))
                {
                    administrator.Name = name;
                }

                if (!administrator.Email.Equals(email))
                {
                    administrator.Email = email;
                    sendEmail           = administrator.Password == null;
                }

                _userManager.Database.SaveChanges();

                Info($"The administrator '{administrator}' is updated!");
            }
            else
            {
                Debug($"The administrator {administrator} doesn't change");
            }

            // check if the administration module is defined and assigned to the user

            ModuleRecord moduleAdministration = null;

            foreach (ModuleRecord record in _userManager.Database.Module.Where(m => m.Module == ModuleRecord.EModule.Administration &&
                                                                               m.Profile == UserProfile.EUserProfile.Administrator &&
                                                                               m.CustomerId == customer.Id).ToList())
            {
                // Module deleted ?

                InformationRecord information = _userManager.Database._Information.Find("Module", record.Id);

                if (information != null && information.IsDeleted)
                {
                    continue;
                }

                moduleAdministration = record;
                if (!moduleAdministration.Enable)
                {
                    Info($"The module administrator '{moduleAdministration}' is enabled!");
                    moduleAdministration.Enable = true;
                    _userManager.Database.SaveChanges();
                }
            }

            if (moduleAdministration == null)
            {
                Debug($"Creation of the module administrator");
                moduleAdministration = new ModuleRecord()
                {
                    Name       = "Administration",
                    Module     = ModuleRecord.EModule.Administration,
                    Profile    = UserProfile.EUserProfile.Administrator,
                    Enable     = true,
                    CustomerId = customer.Id
                };
                _userManager.Database.Module.Add(moduleAdministration);
                _userManager.Database.SaveChanges();
                Info($"Module({moduleAdministration.Id}) created");
            }

            // check if the module administration is assigned to the administrator

            UserModuleRecord userModuleAdministration = null;

            foreach (UserModuleRecord record in _userManager.Database.UserModule.Where(a => a.ModuleId == moduleAdministration.Id &&
                                                                                       a.UserId == administrator.Id &&
                                                                                       a.CustomerId == customer.Id).ToList())
            {
                // Module deleted ?

                InformationRecord information = _userManager.Database._Information.Find("UserModule", record.Id);

                if (information != null && information.IsDeleted)
                {
                    continue;
                }

                userModuleAdministration = record;
            }

            if (userModuleAdministration == null)
            {
                Debug($"Creation of the association between the user and the module administration");
                userModuleAdministration = new UserModuleRecord()
                {
                    ModuleId   = moduleAdministration.Id,
                    UserId     = administrator.Id,
                    CustomerId = customer.Id
                };
                _userManager.Database.UserModule.Add(userModuleAdministration);
                _userManager.Database.SaveChanges();
                Info($"UserModule({userModuleAdministration.Id}) created");
            }

            // update the customer

            customer.Name    = name;
            customer.Login   = login;
            customer.Email   = email;
            customer.Address = address;
            customer.Comment = comment;

            _userManager.Database.SaveChanges();

            if (sendEmail)
            {
                // send a mail for the new user

                Info($"Sending an email to create the password ...");

                using (UserController controller = new UserController(_userManager))
                    controller.SendNewPassword(administrator.Login);
            }

            Info($"Customer updated ...");

            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public ActionResult Add(string name, string login, string email, string address, string comment)
        {
            Debug($"Get ~/Administration/Customer/Add(name={name}, login={login}, email={email}, address={address}, comment={comment})");

            // Only for administrator from the first customer (Syncytium)

            if (!(_userManager.GetById(int.Parse(HttpContext.User.Identity.Name)) is UserRecord user) || user.CustomerId != 1)
            {
                return(HttpNotFound());
            }

            // check the value by itself

            Errors errors = new Errors();

            // no name set

            if (string.IsNullOrWhiteSpace(name))
            {
                errors.AddField("Name", "ERR_FIELD_REQUIRED", new object[] { "{CUSTOMER_NAME}" });
            }
            name = name.Trim();

            // no login set

            if (string.IsNullOrWhiteSpace(login))
            {
                errors.AddField("Login", "ERR_FIELD_REQUIRED", new object[] { "{CUSTOMER_LOGIN}" });
            }
            login = login.Trim();

            // no email set

            if (string.IsNullOrWhiteSpace(email))
            {
                errors.AddField("Email", "ERR_FIELD_REQUIRED", new object[] { "{CUSTOMER_EMAIL}" });
            }
            email = email.Trim();

            // check if the name already exists

            if (_userManager.Database.Customer.Where(c => c.Name.Equals(name)).Any())
            {
                errors.AddField("Name", "ERR_FIELD_UNIQUE", new object[] { "{CUSTOMER_NAME}" });
            }

            // check if the login already exists

            bool loginExist = false;

            foreach (UserRecord record in _userManager.Database.User.Where(u => u.Login.Equals(login)).ToList())
            {
                // User deleted ?

                InformationRecord information = _userManager.Database._Information.FirstOrDefault(info => info.Id == record.Id && info.Table.Equals("User"));
                if (information == null || information.DeleteTick == null)
                {
                    loginExist = true;
                    break;
                }
            }

            if (loginExist)
            {
                errors.AddField("Name", "ERR_FIELD_UNIQUE", new object[] { "{CUSTOMER_LOGIN}" });
            }

            // load ressources before designing the screen fitted to the user's profile

            LanguageDictionary ressources = new LanguageDictionary(Server.MapPath(LanguageDictionary.DIRECTORY_IMAGE), ConfigurationManager.DefaultLanguage);

            ressources.Load(_userManager.Database, user.CustomerId);

            if (errors.HasError)
            {
                // update ModelState on depends on errors

                SetModelState(ModelState, ressources, errors);
                return(View(new CustomerViewModel(ressources, user)));
            }

            // Create a new customer

            Info($"Creating of a new customer ('{name}', '{login}', '{email}', '{address}', '{comment}') ...");

            CustomerRecord newCustomer = new CustomerRecord()
            {
                Name    = name,
                Login   = login,
                Email   = email,
                Address = address,
                Comment = comment
            };

            _userManager.Database.Customer.Add(newCustomer);
            _userManager.Database.SaveChanges();

            Info($"Customer created {newCustomer}");

            // Add the parameter "Language.Tick.<customerId>" into the parameter table

            _userManager.Database._Parameter.Add(new ParameterRecord()
            {
                Key = $"Language.Tick.{newCustomer.Id}", Value = "0"
            });

            // Duplicate multilanguage dictionary (from the customer 1 to the new one)

            Info($"Duplicating multilanguage labels ...");

            int nbLabels = 0;

            foreach (LanguageRecord languageRecord in _userManager.Database.Language.Where(l => l.CustomerId == 1).ToList())
            {
                LanguageRecord newLanguageRecord = LanguageRecord.Copy(languageRecord) as LanguageRecord;
                newLanguageRecord.CustomerId = newCustomer.Id;
                _userManager.Database.Language.Add(newLanguageRecord);
                nbLabels++;
            }

            Info($"{nbLabels} labels duplicated");

            // Create the administrator for this new customer

            UserRecord newUser = new UserRecord()
            {
                Login        = login,
                Registration = name,
                Name         = name,
                Email        = email,
                Language     = user.Language,
                CustomerId   = newCustomer.Id
            };

            _userManager.Database.User.Add(newUser);
            _userManager.Database.SaveChanges();
            Info($"Creating a new user {newUser} ...");

            ModuleRecord newModule = new ModuleRecord()
            {
                Name       = "Administration",
                Module     = ModuleRecord.EModule.Administration,
                Profile    = UserProfile.EUserProfile.Administrator,
                Enable     = true,
                CustomerId = newCustomer.Id
            };

            _userManager.Database.Module.Add(newModule);
            _userManager.Database.SaveChanges();
            Info($"Module({newModule.Id}) created");

            UserModuleRecord newUserModule = new UserModuleRecord()
            {
                UserId     = newUser.Id,
                ModuleId   = newModule.Id,
                Default    = true,
                CustomerId = newCustomer.Id
            };

            _userManager.Database.UserModule.Add(newUserModule);
            _userManager.Database.SaveChanges();
            Info($"UserModule({newUserModule.Id}) created");

            // send a mail for the new user

            Info($"Sending an email to create the password ...");

            using (UserController controller = new UserController(_userManager))
                controller.SendNewPassword(newUser.Login);

            Info($"Customer created ...");

            return(RedirectToAction("Index"));
        }