Exemplo n.º 1
0
        public long AddOrUpdateDepartment(AddDepartmentCriteria departmentCirteria)
        {
            long       retour     = 0;
            Department department = departmentCirteria.department;

            if (department != null)
            {
                Department departmentToUpdateOrSave = null;
                departmentToUpdateOrSave = (department.Id != 0) ? db.Departments.Find(department.Id) : db.Departments.Create();
                departmentToUpdateOrSave.department_name = department.department_name;
                departmentToUpdateOrSave.isWorkshop      = department.isWorkshop;
                db.Entry(departmentToUpdateOrSave).State = (department.Id != 0) ? EntityState.Modified : EntityState.Added;

                List <long> altelierIds = departmentCirteria.altelierIds;
                if (altelierIds.Count() > 0)
                {
                    //Add into table derpatment_altelier
                }

                db.SaveChanges();
                retour = departmentToUpdateOrSave.Id;
            }

            return(retour);
        }
Exemplo n.º 2
0
 private void CreatTypeCar(ManagementDbContext context)
 {
     if (context.TypeCars.Count() == 0)
     {
         List <TypeCar> listTypeCars = new List <TypeCar>()
         {
             new TypeCar()
             {
                 Name = "Xe khách"
             },
             new TypeCar()
             {
                 Name = "Xe hợp đồng"
             },
         };
         context.TypeCars.AddRange(listTypeCars);
         context.SaveChanges();
     }
     if (context.PostCategories.Count() == 0)
     {
         List <PostCategory> lstPostCate = new List <PostCategory>()
         {
             new PostCategory()
             {
                 Name = "Tổng hợp", Status = true, Alias = "Tonghop"
             }
         };
         context.PostCategories.AddRange(lstPostCate);
         context.SaveChanges();
     }
 }
        private void CreateEditions()
        {
            var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);

            if (defaultEdition == null)
            {
                defaultEdition = new Edition {
                    Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName
                };
                _context.Editions.Add(defaultEdition);
                _context.SaveChanges();

                /* Add desired features to the standard edition, if wanted... */
            }
        }
Exemplo n.º 4
0
        public int ModifyUser(long id, string username, string password, int authority)
        {
            int retour = 0;

            using (var db = new ManagementDbContext())
            {
                try
                {
                    var result = (from u in db.Users
                                  where u.Username == username && u.Id != id
                                  select u.Id).FirstOrDefault();

                    if (result == 0)
                    {
                        var user = db.Users.Find(id);
                        user.Username        = username;
                        user.Password        = password;
                        user.Authority       = authority;
                        user.UpdateBy        = Comon.UserId;
                        db.Entry(user).State = EntityState.Modified;
                        //db.SaveChanges();
                        db.SaveChanges();
                        retour = 1;
                    }
                }
                catch (Exception e)
                {
                    retour = 0;
                }
            }
            return(retour);
        }
Exemplo n.º 5
0
        public int Registrer(string username, string password, int authority)
        {
            int retour = 0;

            using (var db = new ManagementDbContext())
            {
                var result = (from u in db.Users
                              where u.Username == username
                              select u.Id).FirstOrDefault();

                if (result == 0)
                {
                    var user = new User
                    {
                        CreateBy = Comon.UserId,
                        Username = username,
                        Password = password,
                        //TODO: Change a more solide methode to get the authority
                        Authority = authority
                    };
                    db.Users.Add(user);
                    db.SaveChanges();
                    retour = 1;
                }
                else
                {
                    retour = 0;
                }
            }
            return(retour);
        }
Exemplo n.º 6
0
        public void Create()
        {
            new DefaultEditionCreator(_context).Create();
            new DefaultLanguagesCreator(_context).Create();
            new HostRoleAndUserCreator(_context).Create();
            new DefaultSettingsCreator(_context).Create();

            _context.SaveChanges();
        }
Exemplo n.º 7
0
        private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            if (_context.Languages.IgnoreQueryFilters().Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }

            _context.Languages.Add(language);
            _context.SaveChanges();
        }
        private void AddSettingIfNotExists(string name, string value, int?tenantId = null)
        {
            if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null))
            {
                return;
            }

            _context.Settings.Add(new Setting(tenantId, null, name, value));
            _context.SaveChanges();
        }
Exemplo n.º 9
0
        public long AddOrUpdateAltelier(Altelier altelier)
        {
            long retour = 0;

            if (altelier != null)
            {
                Altelier altelierToUpdateOrSave = null;
                altelierToUpdateOrSave = (altelier.Id > 0) ? db.Alteliers.Find(altelier.Id) : db.Alteliers.Create();
                altelierToUpdateOrSave.alterlier_name      = altelier.alterlier_name;
                altelierToUpdateOrSave.alterlier_propotion = altelier.alterlier_propotion;


                db.Entry(altelierToUpdateOrSave).State = (altelier.Id > 0) ? EntityState.Modified : EntityState.Added;

                db.SaveChanges();// TODO: change to asyn
                retour = altelierToUpdateOrSave.Id;
            }

            return(retour);
        }
Exemplo n.º 10
0
        public long AddorUpdateEmployee(Employee employee)
        {
            long retour = 0;

            if (employee != null)
            {
                Employee employeeToSaveOrUpdate = null;
                employeeToSaveOrUpdate = (employee.id > 0) ? db.Employees.Find(employee.id) : db.Employees.Create();
                employeeToSaveOrUpdate.employee_name         = employee.employee_name;
                employeeToSaveOrUpdate.altelier_id           = employee.altelier_id;
                employeeToSaveOrUpdate.haveDorm              = employee.haveDorm;
                employeeToSaveOrUpdate.haveSeniorityPay      = employee.haveSeniorityPay;
                employeeToSaveOrUpdate.havePaySocialSecurity = employee.havePaySocialSecurity;
                employeeToSaveOrUpdate.isTeamLeader          = employee.isTeamLeader;
                db.Entry(employeeToSaveOrUpdate).State       = (employee.id > 0) ? EntityState.Modified : EntityState.Added;

                db.SaveChanges();
                retour = employeeToSaveOrUpdate.id;
            }


            return(retour);
        }
Exemplo n.º 11
0
        public long AddOrUpdateUser(User user)
        {
            long retour = 0;

            if (user != null)
            {
                User userToCreateOrUpdate = null;

                userToCreateOrUpdate = (user.Id > 0) ? db.Users.Find(user.Id) : db.Users.Create();
                if (db.Users.Where(p => p.Username == user.Username).FirstOrDefault() != null && user.Id == 0)
                {
                    return(0);
                }
                userToCreateOrUpdate.Username  = user.Username;
                userToCreateOrUpdate.Password  = Comon.GetEncryptText(user.Password);
                userToCreateOrUpdate.Authority = user.Authority;

                db.Entry(userToCreateOrUpdate).State = (user.Id > 0) ? EntityState.Modified : EntityState.Added;
                db.SaveChanges();//TODO: async
                retour = userToCreateOrUpdate.Id;
            }

            return(retour);
        }
Exemplo n.º 12
0
        public long AddOrUpdateRevenue(Revenue revenue)
        {
            long retour = 0;

            if (revenue != null)
            {
                Revenue revenueToCreateOrUpdate = null;

                revenueToCreateOrUpdate = (revenue.id > 0) ? db.Revenues.Find(revenue.id) : db.Revenues.Create();
                if (db.Revenues.Where(p => p.year == revenue.year && p.month == revenue.month).FirstOrDefault() != null && revenue.id == 0)
                {
                    return(0);
                }
                revenueToCreateOrUpdate.year   = revenue.year;
                revenueToCreateOrUpdate.month  = revenue.month;
                revenueToCreateOrUpdate.number = revenue.number;

                db.Entry(revenueToCreateOrUpdate).State = (revenue.id > 0) ? EntityState.Modified : EntityState.Added;
                db.SaveChanges();//TODO: async
                retour = revenueToCreateOrUpdate.id;
            }

            return(retour);
        }
        private void CreateDefaultTenant()
        {
            // Default tenant

            var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == AbpTenantBase.DefaultTenantName);

            if (defaultTenant == null)
            {
                defaultTenant = new Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName);

                var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
                if (defaultEdition != null)
                {
                    defaultTenant.EditionId = defaultEdition.Id;
                }

                _context.Tenants.Add(defaultTenant);
                _context.SaveChanges();
            }
        }
        private void CreateHostRoleAndUsers()
        {
            // Admin role for host

            var adminRoleForHost = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin);

            if (adminRoleForHost == null)
            {
                adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin)
                {
                    IsStatic = true, IsDefault = true
                }).Entity;
                _context.SaveChanges();
            }

            // Grant all permissions to admin role for host

            var grantedPermissions = _context.Permissions.IgnoreQueryFilters()
                                     .OfType <RolePermissionSetting>()
                                     .Where(p => p.TenantId == null && p.RoleId == adminRoleForHost.Id)
                                     .Select(p => p.Name)
                                     .ToList();

            var permissions = PermissionFinder
                              .GetAllPermissions(new ManagementAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host) &&
                                     !grantedPermissions.Contains(p.Name))
                              .ToList();

            if (permissions.Any())
            {
                _context.Permissions.AddRange(
                    permissions.Select(permission => new RolePermissionSetting
                {
                    TenantId  = null,
                    Name      = permission.Name,
                    IsGranted = true,
                    RoleId    = adminRoleForHost.Id
                })
                    );
                _context.SaveChanges();
            }

            // Admin user for host

            var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == AbpUserBase.AdminUserName);

            if (adminUserForHost == null)
            {
                var user = new User
                {
                    TenantId         = null,
                    UserName         = AbpUserBase.AdminUserName,
                    Name             = "admin",
                    Surname          = "admin",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    IsActive         = true
                };

                user.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, "123qwe");
                user.SetNormalizedNames();

                adminUserForHost = _context.Users.Add(user).Entity;
                _context.SaveChanges();

                // Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id));
                _context.SaveChanges();

                _context.SaveChanges();
            }
        }
        private void CreateRolesAndUsers()
        {
            // Admin role

            var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin);

            if (adminRole == null)
            {
                adminRole = _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin)
                {
                    IsStatic = true
                }).Entity;
                _context.SaveChanges();
            }

            // Grant all permissions to admin role

            var grantedPermissions = _context.Permissions.IgnoreQueryFilters()
                                     .OfType <RolePermissionSetting>()
                                     .Where(p => p.TenantId == _tenantId && p.RoleId == adminRole.Id)
                                     .Select(p => p.Name)
                                     .ToList();

            var permissions = PermissionFinder
                              .GetAllPermissions(new ManagementAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant) &&
                                     !grantedPermissions.Contains(p.Name))
                              .ToList();

            if (permissions.Any())
            {
                _context.Permissions.AddRange(
                    permissions.Select(permission => new RolePermissionSetting
                {
                    TenantId  = _tenantId,
                    Name      = permission.Name,
                    IsGranted = true,
                    RoleId    = adminRole.Id
                })
                    );
                _context.SaveChanges();
            }

            // Admin user

            var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == AbpUserBase.AdminUserName);

            if (adminUser == null)
            {
                adminUser                  = User.CreateTenantAdminUser(_tenantId, "*****@*****.**");
                adminUser.Password         = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe");
                adminUser.IsEmailConfirmed = true;
                adminUser.IsActive         = true;

                _context.Users.Add(adminUser);
                _context.SaveChanges();

                // Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id));
                _context.SaveChanges();
            }
        }