Esempio n. 1
0
        public ActionResult Index( string view, string type )
        {
            var list = db.Roles.OrderBy( r => r.Name ).ToList().Select( rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name } ).ToList();
            ViewBag.Roles = list;
            ViewBag.Title = "People";
            ViewBag.ExtendType = "person";
            Person[] AllPersons = db.Users.ToArray();
            Person[][] persons = new Person[][] { getTeachers( AllPersons ), getStudents( AllPersons ) };
            ViewBag.Type = type;

            if( view != null && view == "grid" ) {
                ViewBag.View = "grid";
            }
            return View( persons );
        }
Esempio n. 2
0
 public ActionResult Edit( string id, Person editedPerson )
 {
     Person person = db.Users.Where( p => p.Id == id ).FirstOrDefault();
     if( person != null ) {
         if( person.UserName == User.Identity.Name || User.IsInRole( "admin" ) ) {
             person.First_Name = editedPerson.First_Name;
             person.Middle_Name = editedPerson.Middle_Name;
             person.Second_Name = editedPerson.Second_Name;
             person.PhoneNumber = editedPerson.PhoneNumber;
             person.Email = editedPerson.Email;
             if( editedPerson.Picture.URL != null ) {
                 person.Picture = new Picture {
                     URL = editedPerson.Picture.URL,
                     Name = person.UserName + "picture"
                 };
             } else {
                 person.Picture = db.Picture.Where( p => p.Name == "DefaultPicture" ).First();
             }
         }
     }
     db.SaveChanges();
     return RedirectToAction( "person", new { @id = id } );
 }
Esempio n. 3
0
 private async Task SignInAsync( Person user, bool isPersistent )
 {
     AuthenticationManager.SignOut( DefaultAuthenticationTypes.ExternalCookie );
     var identity = await userManager.CreateIdentityAsync( user, DefaultAuthenticationTypes.ApplicationCookie );
     AuthenticationManager.SignIn( new AuthenticationProperties() { IsPersistent = isPersistent }, identity );
 }
Esempio n. 4
0
        public async Task<ActionResult> ExternalLoginConfirmation( ExternalLoginConfirmationViewModel model, string returnUrl )
        {
            if( User.Identity.IsAuthenticated ) {
                return RedirectToAction( "Manage" );
            }

            if( ModelState.IsValid ) {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if( info == null ) {
                    return View( "ExternalLoginFailure" );
                }
                var user = new Person() { UserName = model.UserName };
                var result = await userManager.CreateAsync( user );
                if( result.Succeeded ) {
                    result = await userManager.AddLoginAsync( user.Id, info.Login );
                    if( result.Succeeded ) {
                        await SignInAsync( user, isPersistent: false );
                        return RedirectToLocal( returnUrl );
                    }
                }
                AddErrors( result );
            }

            ViewBag.ReturnUrl = returnUrl;
            return View( model );
        }
Esempio n. 5
0
        public async Task<ActionResult> Register( RegisterViewModel model )
        {
            if( ModelState.IsValid ) {
                var user = new Person() {
                    UserName = model.UserName,
                    Email = model.UserName,
                    First_Name = model.First_Name,
                    Second_Name = model.Second_Name,
                    Middle_Name = model.Middle_Name,
                    Registration_Date = DateTime.Now,
                    Last_Date_Was_Online = DateTime.Now,
                    PhoneNumber = model.PhoneNumber,
                    Exists = true,
                    Person_Type = model.Person_Type
                };
                var result = await userManager.CreateAsync( user, model.Password );
                db.SaveChanges();
                if( result.Succeeded ) {
                    Person pers = db.Users.Where( p => p.UserName == model.UserName ).FirstOrDefault();
                    pers.Picture = db.Picture.Where( p => p.Name == "DefaultPicture" ).FirstOrDefault();
                    userManager.AddToRole( pers.Id, "user" );
                    db.SaveChanges();
                    await SignInAsync( user, isPersistent: false );

                    // Here mail admins if teacher needs to be approved
                    if( user.Person_Type == "Teacher" ) {
                        pers.Person_Type = "Student"; // roll the status back before the confirmation
                        db.SaveChanges();

                        SendVerificationEmail( user );
                    }

                    List<ModelError> errors = new List<ModelError>();
                    foreach( ModelState modelState in ViewData.ModelState.Values ) {
                        foreach( ModelError error in modelState.Errors ) {
                            errors.Add( error );
                        }
                    }

                    TempData["errors"] = errors;
                    if ( errors.Count() != 0 ) {
                        return RedirectToAction( "Index", "Error" );
                    }

                    return RedirectToAction( "Index", "Home" );
                } else {
                    AddErrors( result );
                }

            }

            // If we got this far, something failed, redisplay form
            return View( model );
        }
Esempio n. 6
0
        protected void SendVerificationEmail( Person user )
        {
            try {
                NameValueCollection mailingSection = ( NameValueCollection )ConfigurationManager.GetSection( "adminMailingSettings" );
                string adminEmail = mailingSection["TargetEmailAddress"].ToString(); // address receiving the confirmation request

                string senderEmail = mailingSection["FromEmailAddress"].ToString(); // address that makes all the mailing
                string senderPasswd = mailingSection["FromEmailPassword"].ToString();
                string senderDisplayName = mailingSection["FromEmailDisplayName"].ToString();

                MailMessage mail = new MailMessage();
                mail.To.Add( adminEmail );
                mail.From = new MailAddress( senderEmail, senderDisplayName, System.Text.Encoding.UTF8 );
                mail.Subject = "[ABBYY Portal] New teacher is waiting for approval";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;

                string bodyTemplate = "Hello!\r\n\r\n" +
                "User {0} has just registered as a teacher and wants to be verified to start working on courses.\r\n\r\n" +
                "If you are sure that you know the user, please, approve it by admin interface. Otherwise you may contact the user at email {1}. \r\n\r\n" +
                "Yours, ABBYY Portal Team.";
                string fullName = String.Format( "{0} {1} {2}", user.First_Name, user.Middle_Name, user.Second_Name );

                mail.Body = String.Format( bodyTemplate, fullName, user.Email );
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = false;
                mail.Priority = MailPriority.High;
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential( senderEmail, senderPasswd );
                client.Port = 587;
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true;
            
                client.Send( mail );
            } catch( Exception ex ) {
                Exception ex2 = ex;
                List<string> errorMessages = new List<string>();
                while( ex2 != null ) {
                    errorMessages.Add( ex2.ToString() );
                    ex2 = ex2.InnerException;
                }
                IdentityResult result = new IdentityResult( errorMessages );
                AddErrors( result );
            }
        }
Esempio n. 7
0
 public ActionResult Index( string SearchFor )
 {
     if( SearchFor != "" ) {
         ViewBag.Title = "People";
         ViewBag.SearchValue = SearchFor;
         var PersonList = db.Users.Where( x => ( x.First_Name + " " + x.Second_Name ).ToUpper().IndexOf( SearchFor.ToUpper() ) >= 0 ||
                                                 ( x.First_Name + " " + x.Middle_Name + " " + x.Second_Name ).ToUpper().IndexOf( SearchFor.ToUpper() ) >= 0
                                                ).Take( 50 ).ToArray();
         Person[][] persons = new Person[][] { getTeachers( PersonList ), getStudents( PersonList ) };
         return View( persons );
     } else {
         var list = db.Roles.OrderBy( r => r.Name ).ToList().Select( rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name } ).ToList();
         ViewBag.Roles = list;
         ViewBag.Title = "People";
         ViewBag.ExtendType = "person";
         Person[] AllPersons = db.Users.ToArray();
         Person[][] persons = new Person[][] { getTeachers( AllPersons ), getStudents( AllPersons ) };
         return View( persons );
     }
 }
Esempio n. 8
0
 private Person[] getTeachers( Person[] persons )
 {
     return persons.Where( p => p.Person_Type == "Teacher" ).ToArray();
 }
Esempio n. 9
0
 private Person[] getStudents( Person[] persons )
 {
     return persons.Where( p => p.Person_Type == "Student" ).ToArray();
 }