Exemple #1
0
        public async Task <IActionResult> CreateAdminUser([FromBody] CaRegisterModel model)
        {
            var UserId   = HttpContext.Session.GetString(UserAccountBusiness.UserAccountSessionkey);
            var hasAdmin = await userAccountBusiness.CheckAdminAccount();

            ApiMessage apiMsg;

            if (!hasAdmin)
            {
                apiMsg = await ApiMessage.Wrap(async() =>
                {
                    var resultTuple = await userAccountBusiness.RegisterUserAdminNamePwd(model);
                    HttpContext.Session.Set
                        (UserAccountBusiness.UserAccountSessionkey,
                        Encoding.UTF8.GetBytes(resultTuple.Item3));
                    await HttpContext.Session.CommitAsync();
                    await SetAuth(resultTuple.Item3);
                    return(resultTuple.Item3);
                });
            }
            else
            {
                var roles = await userAccountBusiness.GetRoles(UserId);

                var hasAdminLogin = roles?.Any(c => c == UserAccountBusiness.AdminUserRoleKey) ?? false;
                if (hasAdminLogin)
                {
                    apiMsg = await ApiMessage.Wrap(async() =>
                    {
                        var resultTuple = await userAccountBusiness.RegisterUserNamePwd(model, UserAccountBusiness.AdminUserRoleKey);
                        HttpContext.Session.Set
                            (UserAccountBusiness.UserAccountSessionkey,
                            Encoding.UTF8.GetBytes(resultTuple.Item3));
                        await HttpContext.Session.CommitAsync();
                        await SetAuth(resultTuple.Item3);
                        return(resultTuple.Item3);
                    });
                }
                else
                {
                    apiMsg = new ApiMessage();
                    apiMsg.SetFault("用户权限不足");
                }
            }

            return(Json(apiMsg));
        }