1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 17 package com.android.vts.entity; 18 19 import com.googlecode.objectify.Key; 20 import com.googlecode.objectify.Ref; 21 import com.googlecode.objectify.annotation.Cache; 22 import com.googlecode.objectify.annotation.Entity; 23 import com.googlecode.objectify.annotation.Id; 24 import com.googlecode.objectify.annotation.Index; 25 import com.googlecode.objectify.annotation.Load; 26 import java.util.ArrayList; 27 import java.util.List; 28 import lombok.EqualsAndHashCode; 29 import lombok.Getter; 30 import lombok.NoArgsConstructor; 31 import lombok.Setter; 32 import java.util.Date; 33 34 import static com.googlecode.objectify.ObjectifyService.ofy; 35 36 /** Entity Class for User */ 37 @Cache 38 @Entity 39 @EqualsAndHashCode(of = "email") 40 @NoArgsConstructor 41 public class UserEntity implements DashboardEntity { 42 43 /** User email field */ 44 @Id @Getter @Setter String email; 45 46 /** User name field */ 47 @Getter @Setter String name; 48 49 /** User name field */ 50 @Getter @Setter String company; 51 52 /** User enable or disable field */ 53 @Index @Getter @Setter Boolean enable; 54 55 /** User admin flag field */ 56 @Index @Getter @Setter Boolean isAdmin; 57 58 /** When this record was created or updated */ 59 @Index @Getter Date updated; 60 61 @Load 62 @Getter 63 List<Ref<RoleEntity>> roles = new ArrayList<>(); 64 65 /** Constructor function for UserEntity Class */ UserEntity( String email, String name, String company, String roleName)66 public UserEntity( 67 String email, 68 String name, 69 String company, 70 String roleName) { 71 72 this.email = email; 73 this.name = name; 74 this.enable = true; 75 this.isAdmin = false; 76 this.company = company; 77 RoleEntity roleEntity = RoleEntity.getRole(roleName); 78 this.roles.add(Ref.create(roleEntity)); 79 } 80 81 /** Saving function for the instance of this class */ 82 @Override save()83 public Key<UserEntity> save() { 84 this.updated = new Date(); 85 return ofy().save().entity(this).now(); 86 } 87 88 /** Get admin user list by admin email */ getAdminUser(String adminEmail)89 public static UserEntity getAdminUser(String adminEmail) { 90 Key key = Key.create(UserEntity.class, adminEmail); 91 return ofy().load() 92 .type(UserEntity.class) 93 .filterKey(key) 94 .filter("enable", true) 95 .filter("isAdmin", true) 96 .first() 97 .now(); 98 } 99 100 /** Get all admin user list */ getAdminUserList()101 public static List<UserEntity> getAdminUserList() { 102 return ofy().load() 103 .type(UserEntity.class) 104 .filter("enable", true) 105 .filter("isAdmin", true) 106 .list(); 107 } 108 109 /** Get normal user list */ getUserList()110 public static List<UserEntity> getUserList() { 111 return ofy().load() 112 .type(UserEntity.class) 113 .filter("enable", true) 114 .filter("isAdmin", false) 115 .list(); 116 } 117 118 /** Get user by email */ getUser(String email)119 public static UserEntity getUser(String email) { 120 Key key = Key.create(UserEntity.class, email); 121 return ofy().load() 122 .type(UserEntity.class) 123 .filterKey(key) 124 .filter("enable", true) 125 .first() 126 .now(); 127 } 128 } 129