Exemple #1
0
 public static void init( bool persist )
 {
     int count = 0;
     using ( ModelContext context = new ModelContext() ) {
         try {
             initWebUser( context );
             initGuests( context );
             initAdmins( context );
             if ( persist ) {
                 count = context.SaveChanges();
                 Log.Debug( $"INIT DataSet persisted COUNT: {count}\nDONE" );
             }
         } catch ( Exception e ) {
             Log.Error( "init: ", e );
         }
     }
 }
Exemple #2
0
 public static void initGuests( ModelContext context )
 {
     DateTime nowWithTime = DateTime.Now + new TimeSpan( 24, 0, 0 );
     Permission perm = generatePermisson( context, "Guest", false );
     Role role = generateRole( context, "Guest" );
     try {
         if ( perm == null || role == null ) {
             throw new Exception( $"Permission is NULL: {role == null} Role is NULL: {perm == null}" );
         } else {
             context.GuestSet.AddOrUpdate( u => u.FirstName, new Guest( perm ) { FirstName = "Guest_1", ExpiryTime = nowWithTime, Role = role } );
             context.GuestSet.AddOrUpdate( u => u.FirstName, new Guest( perm ) { FirstName = "Guest_2", ExpiryTime = nowWithTime, Role = role } );
             context.GuestSet.AddOrUpdate( u => u.FirstName, new Guest( perm ) { FirstName = "Guest_3", ExpiryTime = nowWithTime, Role = role } );
             foreach ( Guest entry in context.GuestSet ) {
                 Log.Debug( $"\tinitGuests: {entry.toString()}\nDONE" );
             }
         }
     } catch ( Exception e ) {
         Log.Error( "initGuests", e );
     }
 }
Exemple #3
0
 public static void initWebUser( ModelContext context )
 {
     Permission perm = generatePermisson( context, "WebUser", true );
     Role role = generateRole( context, "WebUser" );
     try {
         if ( perm == null || role == null ) {
             throw new Exception( string.Format( "Permission is NULL: {0} Role is NULL: {1}", role == null, perm == null ) );
         } else {
             context.WebUserSet.AddOrUpdate( u => u.Email, new WebUser( perm ) {
                 FirstName = "Klara",
                 LastName = "Korn",
                 Email = "*****@*****.**",
                 Role = role,
                 Login = ( new Security.Login( "123" ) { LoginName = "test", LastLogin = DateTime.Now, LastIp = "127.0.0.1" } ),
                 Address = ( new Address() { City = "Göttingen", Street = "Industirestr. 8", Zip = "37079" } )
             } );
             context.WebUserSet.AddOrUpdate( u => u.Email, new WebUser( perm ) {
                 FirstName = "Niko",
                 LastName = "Tin",
                 Email = "*****@*****.**",
                 Role = role,
                 Login = ( new Security.Login( "123" ) { LoginName = "test", LastLogin = DateTime.Now, LastIp = "127.0.0.1" } ),
                 Address = ( new Address() { City = "Göttingen", Street = "Industirestr. 8", Zip = "37079" } )
             } );
             context.WebUserSet.AddOrUpdate( u => u.Email, new WebUser( perm ) {
                 FirstName = "Rita",
                 LastName = "Lin",
                 Email = "*****@*****.**",
                 Role = role,
                 Login = ( new Security.Login( "123" ) { LoginName = "test", LastLogin = DateTime.Now, LastIp = "127.0.0.1" } ),
                 Address = ( new Address() { City = "Göttingen", Street = "Industirestr. 8", Zip = "37079" } )
             } );
             foreach ( WebUser entry in context.WebUserSet ) {
                 Log.Debug( $"\tinitWebUser: {entry.toString()}\nDONE" );
             }
         }
     } catch ( Exception e ) {
         Log.Error( "initWebUser", e );
     }
 }
Exemple #4
0
 private static void initAdmins( ModelContext context )
 {
     Permission perm = generatePermisson( context, "Adminsistrator", true );
     Role role = generateRole( context, "Adminsistrator" );
     try {
         if ( perm == null || role == null ) {
             throw new Exception( string.Format( "Permission is NULL: {0} Role is NULL: {1}", role == null, perm == null ) );
         } else {
             context.SystemUserSet.AddOrUpdate( u => u.Email, new Admin( perm ) {
                 FirstName = "Admin",
                 LastName = "Admin",
                 Email = "*****@*****.**",
                 Role = role,
                 Login = ( new Security.Login( "123" ) { LoginName = "test", LastLogin = DateTime.Now, LastIp = "127.0.0.1" } )
             } );
             foreach ( Admin entry in context.SystemUserSet ) {
                 Log.Debug( $"\tinitAdmins: {entry.toString()}\nDONE" );
             }
         }
     } catch ( Exception e ) {
         Log.Error( "initAdmins", e );
     }
 }
Exemple #5
0
 private static Role generateRole( ModelContext context, string name )
 {
     try {
         Role role = new Role() { RoleName = name };
         context.RoleSet.AddOrUpdate<Role>( role );
         Log.Debug( $"\ngenerateRole: {role.toString()}\nDONE" );
         return role;
     } catch ( Exception e ) {
         Log.Error( "generateRole", e );
         return null;
     }
 }
Exemple #6
0
 private static Permission generatePermisson( ModelContext context, string name, bool state )
 {
     try {
         Permission permission = new Permission() {
             Name = name,
             Credential = generateCredantial( context, state ),
             Task = generateTask( name )
         };
         context.PermissionSet.AddOrUpdate( p => p.Name, permission );
         Log.DebugFormat( $"generatePermisson: {permission.toString()}\nDONE" );
         return permission;
     } catch ( Exception e ) {
         Log.Error( "generatePermisson", e );
         return null;
     }
 }
Exemple #7
0
 private static Credential generateCredantial( ModelContext context, bool state )
 {
     try {
         Credential credential = new Credential() {
             CanCreate = state,
             CanRead = state,
             CanUpdate = state,
             CanDelete = state
         };
         context.CredentialSet.AddOrUpdate( credential );
         Log.Debug( $"generateCredantial: {credential.toString()}\nDONE" );
         return credential;
     } catch ( DbEntityValidationException e ) {
         Log.Error( "generateCredantial", e );
         return null;
     } catch ( Exception e ) {
         Log.Error( "generateCredantial", e );
         return null;
     }
 }
Exemple #8
0
        public static void createXml(string path)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(path, settings);
            writer.WriteStartDocument();

            using (ModelContext context = new ModelContext()) {
                writer.WriteStartElement("Users");

                writer.WriteStartElement("WebUsers");
                foreach (WebUser entry in context.WebUserSet.Include("Permissions").Include("Role").Include("Login").Include("Permissions.Task").Include("Permissions.Credential")) {
                    writer.WriteStartElement("WebUser");
                    writer.WriteAttributeString("FirstName", entry.FirstName);
                    writer.WriteAttributeString("LastName", entry.LastName);
                    writer.WriteAttributeString("Email", entry.Email);

                    writer.WriteStartElement("Permissions");
                    foreach (Permission p in entry.Permissions) {
                        writer.WriteStartElement("Permission");
                        writer.WriteAttributeString("Name", p.Name);

                        writer.WriteStartElement("Task");
                        writer.WriteAttributeString("Name", p.Task.Name);
                        writer.WriteElementString("Description", p.Task.Description);
                        writer.WriteEndElement(); // Task End

                        writer.WriteStartElement("Credential");
                        writer.WriteAttributeString("Create", $"{p.Credential.CanCreate}");
                        writer.WriteAttributeString("Read", $"{p.Credential.CanRead}");
                        writer.WriteAttributeString("Update", $"{p.Credential.CanUpdate}");
                        writer.WriteAttributeString("CreateDelete", $"{p.Credential.CanDelete}");
                        writer.WriteEndElement(); // Credential End

                        writer.WriteEndElement(); // Permission End
                    }
                    writer.WriteEndElement(); // Permissions End

                    writer.WriteStartElement("Role");
                    writer.WriteAttributeString("Name", entry.Role.RoleName);
                    writer.WriteEndElement(); // Role End

                    writer.WriteStartElement("Login");
                    writer.WriteAttributeString("LoginName", entry.Login.LoginName);
                    writer.WriteAttributeString("Password", "123");
                    writer.WriteEndElement(); // Login End

                    writer.WriteEndElement(); // WebUser End
                }
                writer.WriteEndElement(); // WebUsers End

                writer.WriteStartElement("Guests");
                foreach (Guest entry in context.GuestSet.Include("Permissions").Include("Role").Include("Permissions.Task").Include("Permissions.Credential")) {
                    writer.WriteStartElement("Guest");
                    writer.WriteAttributeString("FirstName", entry.FirstName);
                    writer.WriteAttributeString("LastName", entry.LastName);
                    writer.WriteAttributeString("Email", entry.Email);

                    writer.WriteStartElement("Permissions");
                    foreach (Permission p in entry.Permissions) {
                        writer.WriteStartElement("Permission");
                        writer.WriteAttributeString("Name", p.Name);

                        writer.WriteStartElement("Task");
                        writer.WriteAttributeString("Name", p.Task.Name);
                        writer.WriteElementString("Description", p.Task.Description);
                        writer.WriteEndElement(); // Task End

                        writer.WriteStartElement("Credential");
                        writer.WriteAttributeString("Create", $"{p.Credential.CanCreate}");
                        writer.WriteAttributeString("Read", $"{p.Credential.CanRead}");
                        writer.WriteAttributeString("Update", $"{p.Credential.CanUpdate}");
                        writer.WriteAttributeString("CreateDelete", $"{p.Credential.CanDelete}");
                        writer.WriteEndElement(); // Credential End

                        writer.WriteEndElement(); // Permission End
                    }
                    writer.WriteEndElement(); // Permissions End

                    writer.WriteStartElement("Role");
                    writer.WriteAttributeString("Name", entry.Role.RoleName);
                    writer.WriteEndElement(); // Role End

                    writer.WriteEndElement(); // Guest End
                }
                writer.WriteEndElement(); // Guests End

                writer.WriteStartElement("Admins");
                foreach (Admin entry in context.SystemUserSet.Include("Permissions").Include("Role").Include("Login").Include("Permissions.Task").Include("Permissions.Credential")) {
                    writer.WriteStartElement("Admin");
                    writer.WriteAttributeString("FirstName", entry.FirstName);
                    writer.WriteAttributeString("LastName", entry.LastName);
                    writer.WriteAttributeString("Email", entry.Email);

                    writer.WriteStartElement("Permissions");
                    foreach (Permission p in entry.Permissions) {
                        writer.WriteStartElement("Permission");
                        writer.WriteAttributeString("Name", p.Name);

                        writer.WriteStartElement("Task");
                        writer.WriteAttributeString("Name", p.Task.Name);
                        writer.WriteElementString("Description", p.Task.Description);
                        writer.WriteEndElement(); // Task End

                        writer.WriteStartElement("Credential");
                        writer.WriteAttributeString("Create", $"{p.Credential.CanCreate}");
                        writer.WriteAttributeString("Read", $"{p.Credential.CanRead}");
                        writer.WriteAttributeString("Update", $"{p.Credential.CanUpdate}");
                        writer.WriteAttributeString("CreateDelete", $"{p.Credential.CanDelete}");
                        writer.WriteEndElement(); // Credential End

                        writer.WriteEndElement(); // Permission End
                    }
                    writer.WriteEndElement(); // Permissions End

                    writer.WriteStartElement("Role");
                    writer.WriteAttributeString("Name", entry.Role.RoleName);
                    writer.WriteEndElement(); // Role End

                    writer.WriteStartElement("Login");
                    writer.WriteAttributeString("LoginName", entry.Login.LoginName);
                    writer.WriteAttributeString("Password", "123");
                    writer.WriteEndElement(); // Login End

                    writer.WriteEndElement(); // Admin End
                }
                writer.WriteEndElement(); // Admins End

                writer.WriteEndElement(); // Users End

                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
        }