示例#1
0
        public MembershipCreateStatus CreateUser(RegisterModel model)
        {
            string userName = model.UserName;
            string password = model.Password;
            string email = model.Email;

            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");

            if (DBEntities.UserSet.Count(u => u.Username == userName) > 0)
            {
                return MembershipCreateStatus.DuplicateUserName;
            }

            User newUser = new User();
            newUser.Username = userName;
            newUser.ChangePassword(password);
            newUser.Mail = email;
            newUser.Name = model.Name;
            newUser.Surname = model.Surname;
            newUser.Created = DateTime.Now;
            newUser.LastLogged = DateTime.Now;
            newUser.ICQ = model.ICQ;
            newUser.Phone = model.Phone;
            newUser.SchoolEmail = model.SchoolEmail;
            newUser.Specialization = model.Specialization;
            newUser.Title = model.Title;
            newUser.StudyProgramme = model.StudyProgramme;
            newUser.TitleAfter = model.TitleAfter;

            DBEntities.UserSet.AddObject(newUser);
            DBEntities.SaveChanges();

            return MembershipCreateStatus.Success;
            // TODO spojit tyto dvě metody
        }
示例#2
0
        public ActionResult Register(RegisterModel model)
        {
            Felbook.Helpers.ImageHelper imageOperator = new Felbook.Helpers.ImageHelper(); //pomocná třída pro operace s obrázky
            HttpPostedFileBase imageToUpload = Request.Files["profileimage"];
            bool uploadImage = false;

            if (imageToUpload.ContentLength == 0)
            {
                uploadImage = false;
            }
            else if (Felbook.Helpers.ImageHelper.IsImage(imageToUpload.ContentType))
            {
                uploadImage = true;
            }
            else {
                ModelState.AddModelError("file", "Your file wasn't image.");
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model);
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                    //upload user profile image
                    User actualUser = Model.UserService.FindByUsername(model.UserName);
                    int userId = actualUser.Id;
                    string fileDir = "../Web_Data/profile_images/";
                    //název souboru je vždy stejný
                    string fileName = "profileimage.png";
                    string fileFullPath = Path.Combine(HttpContext.Server.MapPath(fileDir + userId), fileName);
                    string fileDirPath = Path.GetDirectoryName(fileFullPath);

                    try
                    {
                        //pokusíme se vytvořit adresář
                        Directory.CreateDirectory(fileDirPath);
                    }
                    //jednotlivě odchytávám chyby
                    catch (UnauthorizedAccessException)
                    {
                        ModelState.AddModelError("file", "Upload wasn´t successful");
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("file", "Some uknown error");
                    }

                    if (uploadImage == true)
                    {
                        imageOperator.ImageResize(imageToUpload, fileFullPath, 90, 120);
                    }
                    else
                    {
                        //zjistím si cesty k souboru
                        string sourceFile = Path.Combine(HttpContext.Server.MapPath(fileDir + "/default/"), fileName);
                        string destFile = System.IO.Path.Combine(fileDirPath, fileName);

                        //kopíruje to soubor
                        System.IO.File.Copy(sourceFile, destFile, true);
                    }
                    return RedirectToAction("Index", "Profile", new { username = actualUser.Username });
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }
示例#3
0
 public MembershipCreateStatus CreateUser(RegisterModel model)
 {
     return CreateUser(model.UserName, model.Password, model.Email);
 }