Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void SendEmailToFindPwd(HttpContext context, OleDbConnection conn)
        {
            string UserName  = context.Request["UserName"];
            string UserEmail = context.Request["UserEmail"];

            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(UserEmail))
            {
                context.Response.Write("请补全信息!");
            }
            else
            {
                // 根据用户名取用户
                Dal.Models.UserInfo     user = BLL.User.GetUserByName(UserName, conn);
                Dal.Models.Organization org;
                if (user == null)
                {
                    // 取单位信息
                    org = BLL.Organization.GetOrganizationByName(UserName, conn);
                    if (org == null)
                    {
                        org = BLL.Organization.GetOrganizationByCode(UserName, conn);
                    }

                    if (org != null)
                    {
                        user = BLL.User.GetUserInfo(org.UserID.Value, conn);
                        if (string.IsNullOrEmpty(user.Email))
                        {
                            user.Email = org.EmailAddress;
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(user.Email) && user.UserType == "0806")
                    {
                        // 取单位信息
                        org        = BLL.Organization.GetOrganizationByUser(user.UserID.Value, conn);
                        user.Email = org.EmailAddress;
                    }
                }

                if (user == null)
                {
                    context.Response.Write("该用户不存在!");
                }
                else
                {
                    if (user.Email != UserEmail)
                    {
                        context.Response.Write("用户名和邮箱不匹配!");
                    }
                    else
                    {
                        Dal.Models.MailInfo mailinfo = new Dal.Models.MailInfo();
                        string strUrl = context.Request.Url.Host + ":" + context.Request.Url.Port + "/View/UserManage/RetrievePassword_Return?id=" + BLL.Encryption.Encrypt(user.UserID.Value.ToString());//加密
                        mailinfo.Subject = "修改密码";

                        Dictionary <string, string> dicAppSetting = BLL.SysConstant.GetAppSettingDict("", conn);
                        mailinfo.SenderAddress = dicAppSetting["SenderAddress"];
                        mailinfo.SmtpAcount    = dicAppSetting["SmtpAcount"];
                        mailinfo.SmtpPassword  = dicAppSetting["SmtpPassword"];

                        mailinfo.RecipientAddress.Add(UserEmail);
                        mailinfo.IsBodyHtml = true;
                        mailinfo.Content    = "请登录页面:<a href='" + strUrl + "' >" + strUrl + "</a>   完成密码修改";
                        try
                        {
                            BLL.Common.SendMail(mailinfo);
                            context.Response.Write("发送成功!");
                        }
                        catch
                        {
                            context.Response.Write("邮箱不存在!");
                        }
                    }
                }
            }
        }