Esempio n. 1
0
        public async Task <ActionResult> proc()
        {
            var json = new StreamReader(Request.Body).ReadToEnd();
            var data = JsonConvert.DeserializeObject <ApplicationUser>(json);

            if (data.Id != null && data.Id != "")
            {
                // Update Profile
                var record = await UserBLL.Update_User_Profile(_context, data, data.isadmin);

                /* attribute processing */
                foreach (var attr in data.attr_values)
                {
                    attr.userid    = data.Id;
                    attr.attr_type = (byte)Attr_Type.UserProfile;
                    if (attr.id > 0)
                    {
                        /* update record */
                        await AttrValueBLL.Update(_context, attr);
                    }
                    else
                    {
                        /* add attribute */
                        if (attr.value != "")
                        {
                            await AttrValueBLL.Add(_context, attr);
                        }
                    }
                }

                record.img_url = UserUrlConfig.ProfilePhoto(record.UserName, record.picturename, 0);
                return(Ok(new { status = "success", record = record, message = SiteConfig.generalLocalizer["_record_updated"].Value }));
            }
            else
            {
                // Create New Account
                var user = new ApplicationUser
                {
                    UserName   = data.UserName,
                    Email      = data.Email,
                    created_at = DateTime.Now,
                    isenabled  = 1, // internal use only (to suspend user account and all posted data at once)
                    firstname  = data.firstname,
                    lastname   = data.lastname
                };

                var result = await SiteConfig.userManager.CreateAsync(user, data.password);

                if (result.Succeeded)
                {
                    // role process
                    if (data.role_name != null && data.role_name != "")
                    {
                        var roleExist = await SiteConfig.roleManager.RoleExistsAsync(data.role_name);

                        if (!roleExist)
                        {
                            ApplicationRole role = new ApplicationRole();
                            role.Name        = data.role_name;
                            role.CreatedDate = DateTime.Now;
                            await SiteConfig.roleManager.CreateAsync(role);
                        }

                        await SiteConfig.userManager.AddToRoleAsync(user, data.role_name);
                    }

                    // Init User Profile
                    await UserProfileBLL.InitializeUserProfile(_context, user);

                    Directory_Process.CreateRequiredDirectories(SiteConfig.Environment.ContentRootPath + UtilityBLL.ParseUsername(SystemDirectoryPaths.UserDirectory, user.Id.ToString()));

                    // enable account directly
                    UserBLL.Update_Field_Id(_context, user.Id, "EmailConfirmed", true);

                    // setup url / picture url for app use only
                    data.Id             = user.Id;
                    data.picturename    = "none";
                    data.LockoutEnabled = false;
                    data.EmailConfirmed = true;
                    data.img_url        = UserUrlConfig.ProfilePhoto(data.UserName, data.picturename, 0); // default set
                    data.url            = UserUrlConfig.ProfileUrl(data, Configs.RegistrationSettings.uniqueFieldOption);

                    return(Ok(new { status = "success", record = data, message = SiteConfig.generalLocalizer["_account_created"].Value }));
                }
                else
                {
                    return(Ok(new { status = "error", record = data, message = SiteConfig.generalLocalizer["_account_failed"].Value }));
                }
            }
        }