Exemplo n.º 1
0
        public async Task <auth_user> Create(auth_user user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_context.auth_users.Any(x => x.username == user.username))
            {
                throw new AppException("Username '" + user.username + "' is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.password_hash = passwordHash;
            user.password_salt = passwordSalt;

            await _context.auth_users.AddAsync(user);

            _context.SaveChanges();

            return(user);
        }
        /// <summary>
        /// 邮箱激活用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string ActivateUser(string id)
        {
            return(Broker.ExecuteTransaction(() =>
            {
                var data = GetData(id);
                if (data == null)
                {
                    return "激活失败";
                }

                if (data.expire_time < DateTime.Now)
                {
                    return "激活失败,激活链接已过期";
                }

                #region 创建用户
                var model = JsonConvert.DeserializeObject <LoginRequest>(data.login_request.ToString());
                var role = new SysRoleService(Broker).GetGuest();
                var user = new user_info()
                {
                    Id = Guid.NewGuid().ToString(),
                    code = model.code,
                    password = model.password,
                    name = model.code.Split("@")[0],
                    mailbox = model.code,
                    roleid = role.Id,
                    roleidName = role.name,
                    stateCode = 1,
                    stateCodeName = "启用"
                };
                Broker.Create(user, false);
                var _authUser = new auth_user()
                {
                    Id = user.user_infoId,
                    name = user.name,
                    code = user.code,
                    roleid = user.roleid,
                    roleidName = user.roleidName,
                    user_infoid = user.user_infoId,
                    is_lock = false,
                    is_lockName = "否",
                    last_login_time = DateTime.Now,
                    password = model.password
                };
                Broker.Create(_authUser);
                #endregion

                data.is_active = true;
                Broker.Update(data);

                return "激活成功";
            }));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建用户认证信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="broker"></param>
        private void CreateAuthInfo(BaseEntity entity, IPersistBroker broker)
        {
            var authInfo = new auth_user()
            {
                auth_userId = entity.GetAttributeValue <string>("user_infoId"),
                name        = entity.GetAttributeValue <string>("name"),
                code        = entity.GetAttributeValue <string>("code"),
                password    = SystemConfig.Config.DefaultPassword,
                user_infoid = entity.GetAttributeValue <string>("user_infoId"),
                roleid      = entity.GetAttributeValue <string>("roleid"),
                roleidName  = entity.GetAttributeValue <string>("roleidName"),
                is_lock     = false,
                is_lockName = "否"
            };

            new AuthUserService(broker).CreateData(authInfo);
        }
Exemplo n.º 4
0
        public void Update(auth_user userParam, string password = null)
        {
            var user = _context.auth_users.Find(userParam.id);

            if (user == null)
            {
                throw new AppException("User not found");
            }

            if (userParam.username != user.username)
            {
                // username has changed so check if the new username is already taken
                if (_context.auth_users.Any(x => x.username == userParam.username))
                {
                    throw new AppException("Username " + userParam.username + " is already taken");
                }
            }

            // update user properties
            //user.GivenName = userParam.GivenName;
            //user.Surname = userParam.Surname;
            user.username = userParam.username;

            // update password if it was entered
            if (!string.IsNullOrWhiteSpace(password))
            {
                byte[] passwordHash, passwordSalt;
                CreatePasswordHash(password, out passwordHash, out passwordSalt);

                user.password_hash = passwordHash;
                user.password_salt = passwordSalt;
            }

            _context.auth_users.Update(user);
            _context.SaveChanges();
        }
Exemplo n.º 5
0
        public static void Initialize(DataContext context)
        {
            context.Database.EnsureCreated();

            //===EXECUTE ALL SQL FILES IN root/Schema TO DB
            //===DEV ONLY
            var    path      = AppDomain.CurrentDomain.BaseDirectory;
            string schemaDir = Path.GetDirectoryName(path); //without file name

            schemaDir = Path.GetDirectoryName(schemaDir);   // Temp folder
            schemaDir = Path.GetDirectoryName(schemaDir);
            schemaDir = Path.GetDirectoryName(schemaDir) + @"/Schema/";
            //string schemaFile = Path.GetDirectoryName(schemaDir) + @"/Schema/Functions/get_lookup.sql";

            string[] directories = Directory.GetDirectories(schemaDir);

            foreach (string s in directories)
            {
                string[] files = Directory.GetFiles(s);

                foreach (string a in files)
                {
                    if (a.Contains(".sql"))
                    {
                        string file = File.OpenText(a).ReadToEnd();
                        context.Database.ExecuteSqlCommand(file);
                    }
                }
            }

            //===END

            // Look for any students.
            if (context.auth_users.Any())
            {
                return;   // DB has been seeded
            }


            byte[] passwordHash, passwordSalt;
            var    password = "******";

            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            var user = new auth_user[]
            {
                new auth_user
                {
                    username              = "******",
                    password_hash         = passwordHash,
                    password_salt         = passwordSalt,
                    security_user_role_id = 1
                }     // 1-admin
            };

            foreach (auth_user s in user)
            {
                context.auth_users.Add(s);
            }

            var authUserRole = new auth_user_role[]
            {
                new auth_user_role {
                    name = "Administrator"
                },
                new auth_user_role {
                    name = "User"
                },
                new auth_user_role {
                    name = "Employee"
                }
            };

            foreach (auth_user_role s in authUserRole)
            {
                context.auth_user_roles.Add(s);
            }

            context.SaveChanges();



            //var enrollments = new Enrollment[]
            //{
            //    new Enrollment {
            //        StudentID = students.Single(s => s.LastName == "Alexander").ID,
            //        CourseID = courses.Single(c => c.Title == "Chemistry" ).CourseID,
            //        Grade = Grade.A
            //    },
            //        new Enrollment {
            //        StudentID = students.Single(s => s.LastName == "Alexander").ID,
            //        CourseID = courses.Single(c => c.Title == "Microeconomics" ).CourseID,
            //        Grade = Grade.C
            //        },
            //        new Enrollment {
            //        StudentID = students.Single(s => s.LastName == "Alexander").ID,
            //        CourseID = courses.Single(c => c.Title == "Macroeconomics" ).CourseID,
            //        Grade = Grade.B
            //        }
            //};

            //foreach (Enrollment e in enrollments)
            //{
            //    var enrollmentInDataBase = context.Enrollments.Where(
            //        s =>
            //                s.Student.ID == e.StudentID &&
            //                s.Course.CourseID == e.CourseID).SingleOrDefault();
            //    if (enrollmentInDataBase == null)
            //    {
            //        context.Enrollments.Add(e);
            //    }
            //}
            //context.SaveChanges();
        }