Пример #1
0
        /// <summary>
        /// 创建新用户(管理员、二级用户)
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int CreateUser(Dal.Models.UserInfo user, OleDbConnection conn, OleDbTransaction tran = null)
        {
            StringBuilder sbSql = new StringBuilder();
            int?          iResult;

            if (user == null ||
                user.CreateUser == null ||
                string.IsNullOrEmpty(user.UserName))
            {
                throw new Exception("用户信息不全!");
            }

            if (string.IsNullOrEmpty(user.UserType))
            {
                Dal.Models.UserInfo userCreator = GetUserInfo(user.CreateUser.Value, conn, tran);
                if (userCreator.UserType == "0801")
                {
                    // 超级管理员创建系统管理员和协会管理员,默认系统管理员
                    user.UserType = "0802";
                }
                else if (userCreator.UserType == "0802")
                {
                    // 系统管理员创建协会管理员
                    user.UserType = "0803";
                }
                else if (userCreator.UserType == "0803")
                {
                    // 协会管理员创建管理员
                    user.UserType = "0804";
                }
                else if (userCreator.UserType == "0804")
                {
                    // 管理员创建会员用户
                    user.UserType = "0806";
                }
                else if (userCreator.UserType == "0806")
                {
                    // 一级用户创建二级用户
                    user.UserType = "0807";
                }
                else
                {
                    // 参数错误
                    throw new Exception("参数错误!");
                }
            }

            // 判断用户名是否存在
            user.UserName = user.UserName.Trim();
            if (GetUserByName(user.UserName, conn, tran) != null ||
                BLL.Organization.GetOrganizationByName(user.UserName, conn, tran) != null ||
                BLL.Organization.GetOrganizationByCode(user.UserName, conn, tran) != null)
            {
                // 用户名已存在,
                throw new Exception("用户名已存在,不可再次创建!");
            }

            user.Email = user.Email.Trim();
            if (!string.IsNullOrEmpty(user.Email))
            {
                if (!Common.IsValidEmail(user.Email))
                {
                    // 邮箱格式错误
                    throw new Exception("邮箱格式错误,请重新填写!");
                }

                if (GetUserByMail(user.Email, conn, tran) != null)
                {
                    // 邮箱已存在,
                    throw new Exception("邮箱已存在,请换其他邮箱!");
                }
            }

            sbSql.Append("  INSERT INTO Users ( ");
            sbSql.Append("          UserName ");
            sbSql.Append("         ,Password ");
            sbSql.Append("         ,UserType ");
            sbSql.Append("         ,Email    ");
            sbSql.Append("         ,UserStatus ");
            sbSql.Append("         ,CreateTime ");
            sbSql.Append("         ,CreateUser ");
            sbSql.Append("         ,LastUpdateTime ");
            sbSql.Append("         ,IsPasswordChanged ");
            sbSql.Append(" ) VALUES ( ?, dbo.Fn_MD5Encrypt(?), ?, ?, '0701', GetDate(), ?, GetDate(),0) ");

            iResult = Dal.OleDbHlper.ExecuteNonQuery(sbSql.ToString(), conn, CommandType.Text, tran
                                                     , new OleDbParameter("@UserName", OleDbType.VarWChar)
            {
                Value = user.UserName
            }
                                                     , new OleDbParameter("@Password", OleDbType.VarWChar)
            {
                Value = user.Password
            }
                                                     , new OleDbParameter("@UserType", OleDbType.VarWChar)
            {
                Value = user.UserType
            }
                                                     , new OleDbParameter("@Email", OleDbType.VarWChar)
            {
                Value = user.Email
            }
                                                     , new OleDbParameter("@CreateUser", OleDbType.Integer)
            {
                Value = user.CreateUser
            }
                                                     );

            user.UserID = GetUserByName(user.UserName, conn, tran).UserID;

            Dictionary <string, string> dictAppSettings = SysConstant.GetAppSettingDict("", conn, tran);
            string strSendMailWhenCreateUser            = dictAppSettings["SendMailWhenCreateUser"];

            if (strSendMailWhenCreateUser.ToLower() == "true")
            {
                Dal.Models.MailInfo mailinfo = new Dal.Models.MailInfo();
                mailinfo.Subject       = "";
                mailinfo.SenderAddress = dictAppSettings["SenderAddress"];
                mailinfo.SmtpAcount    = dictAppSettings["SmtpAcount"];
                mailinfo.SmtpPassword  = dictAppSettings["SmtpPassword"];
                mailinfo.RecipientAddress.Add(user.Email);
                mailinfo.IsBodyHtml = true;
                mailinfo.Content    = "您已成功注册" + dictAppSettings["SystemName"] + "系统的账号!";
                Common.SendMail(mailinfo);
            }

            return(user.UserID.Value);
        }